<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
</head>
<body>
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span>/*<a name="line.1"></a>
<span class="sourceLineNo">002</span> * Copyright (C) 2007 The Guava Authors<a name="line.2"></a>
<span class="sourceLineNo">003</span> *<a name="line.3"></a>
<span class="sourceLineNo">004</span> * Licensed under the Apache License, Version 2.0 (the "License");<a name="line.4"></a>
<span class="sourceLineNo">005</span> * you may not use this file except in compliance with the License.<a name="line.5"></a>
<span class="sourceLineNo">006</span> * You may obtain a copy of the License at<a name="line.6"></a>
<span class="sourceLineNo">007</span> *<a name="line.7"></a>
<span class="sourceLineNo">008</span> * http://www.apache.org/licenses/LICENSE-2.0<a name="line.8"></a>
<span class="sourceLineNo">009</span> *<a name="line.9"></a>
<span class="sourceLineNo">010</span> * Unless required by applicable law or agreed to in writing, software<a name="line.10"></a>
<span class="sourceLineNo">011</span> * distributed under the License is distributed on an "AS IS" BASIS,<a name="line.11"></a>
<span class="sourceLineNo">012</span> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<a name="line.12"></a>
<span class="sourceLineNo">013</span> * See the License for the specific language governing permissions and<a name="line.13"></a>
<span class="sourceLineNo">014</span> * limitations under the License.<a name="line.14"></a>
<span class="sourceLineNo">015</span> */<a name="line.15"></a>
<span class="sourceLineNo">016</span><a name="line.16"></a>
<span class="sourceLineNo">017</span>package com.google.common.collect;<a name="line.17"></a>
<span class="sourceLineNo">018</span><a name="line.18"></a>
<span class="sourceLineNo">019</span>import static com.google.common.base.Preconditions.checkArgument;<a name="line.19"></a>
<span class="sourceLineNo">020</span>import static com.google.common.base.Preconditions.checkNotNull;<a name="line.20"></a>
<span class="sourceLineNo">021</span>import static com.google.common.base.Preconditions.checkState;<a name="line.21"></a>
<span class="sourceLineNo">022</span><a name="line.22"></a>
<span class="sourceLineNo">023</span>import com.google.common.annotations.GwtCompatible;<a name="line.23"></a>
<span class="sourceLineNo">024</span>import com.google.common.annotations.GwtIncompatible;<a name="line.24"></a>
<span class="sourceLineNo">025</span>import com.google.common.base.Function;<a name="line.25"></a>
<span class="sourceLineNo">026</span>import com.google.common.base.Joiner;<a name="line.26"></a>
<span class="sourceLineNo">027</span>import com.google.common.base.Joiner.MapJoiner;<a name="line.27"></a>
<span class="sourceLineNo">028</span>import com.google.common.base.Objects;<a name="line.28"></a>
<span class="sourceLineNo">029</span>import com.google.common.base.Predicate;<a name="line.29"></a>
<span class="sourceLineNo">030</span>import com.google.common.base.Predicates;<a name="line.30"></a>
<span class="sourceLineNo">031</span>import com.google.common.base.Supplier;<a name="line.31"></a>
<span class="sourceLineNo">032</span>import com.google.common.collect.Collections2.TransformedCollection;<a name="line.32"></a>
<span class="sourceLineNo">033</span>import com.google.common.collect.Maps.EntryTransformer;<a name="line.33"></a>
<span class="sourceLineNo">034</span><a name="line.34"></a>
<span class="sourceLineNo">035</span>import java.io.IOException;<a name="line.35"></a>
<span class="sourceLineNo">036</span>import java.io.ObjectInputStream;<a name="line.36"></a>
<span class="sourceLineNo">037</span>import java.io.ObjectOutputStream;<a name="line.37"></a>
<span class="sourceLineNo">038</span>import java.io.Serializable;<a name="line.38"></a>
<span class="sourceLineNo">039</span>import java.util.AbstractCollection;<a name="line.39"></a>
<span class="sourceLineNo">040</span>import java.util.Collection;<a name="line.40"></a>
<span class="sourceLineNo">041</span>import java.util.Collections;<a name="line.41"></a>
<span class="sourceLineNo">042</span>import java.util.Comparator;<a name="line.42"></a>
<span class="sourceLineNo">043</span>import java.util.HashSet;<a name="line.43"></a>
<span class="sourceLineNo">044</span>import java.util.Iterator;<a name="line.44"></a>
<span class="sourceLineNo">045</span>import java.util.List;<a name="line.45"></a>
<span class="sourceLineNo">046</span>import java.util.Map;<a name="line.46"></a>
<span class="sourceLineNo">047</span>import java.util.Map.Entry;<a name="line.47"></a>
<span class="sourceLineNo">048</span>import java.util.NoSuchElementException;<a name="line.48"></a>
<span class="sourceLineNo">049</span>import java.util.Set;<a name="line.49"></a>
<span class="sourceLineNo">050</span>import java.util.SortedSet;<a name="line.50"></a>
<span class="sourceLineNo">051</span><a name="line.51"></a>
<span class="sourceLineNo">052</span>import javax.annotation.Nullable;<a name="line.52"></a>
<span class="sourceLineNo">053</span><a name="line.53"></a>
<span class="sourceLineNo">054</span>/**<a name="line.54"></a>
<span class="sourceLineNo">055</span> * Provides static methods acting on or generating a {@code Multimap}.<a name="line.55"></a>
<span class="sourceLineNo">056</span> *<a name="line.56"></a>
<span class="sourceLineNo">057</span> * &lt;p&gt;See the Guava User Guide article on &lt;a href=<a name="line.57"></a>
<span class="sourceLineNo">058</span> * "http://code.google.com/p/guava-libraries/wiki/CollectionUtilitiesExplained#Multimaps"&gt;<a name="line.58"></a>
<span class="sourceLineNo">059</span> * {@code Multimaps}&lt;/a&gt;.<a name="line.59"></a>
<span class="sourceLineNo">060</span> *<a name="line.60"></a>
<span class="sourceLineNo">061</span> * @author Jared Levy<a name="line.61"></a>
<span class="sourceLineNo">062</span> * @author Robert Konigsberg<a name="line.62"></a>
<span class="sourceLineNo">063</span> * @author Mike Bostock<a name="line.63"></a>
<span class="sourceLineNo">064</span> * @author Louis Wasserman<a name="line.64"></a>
<span class="sourceLineNo">065</span> * @since 2.0 (imported from Google Collections Library)<a name="line.65"></a>
<span class="sourceLineNo">066</span> */<a name="line.66"></a>
<span class="sourceLineNo">067</span>@GwtCompatible(emulated = true)<a name="line.67"></a>
<span class="sourceLineNo">068</span>public final class Multimaps {<a name="line.68"></a>
<span class="sourceLineNo">069</span>  private Multimaps() {}<a name="line.69"></a>
<span class="sourceLineNo">070</span><a name="line.70"></a>
<span class="sourceLineNo">071</span>  /**<a name="line.71"></a>
<span class="sourceLineNo">072</span>   * Creates a new {@code Multimap} backed by {@code map}, whose internal value<a name="line.72"></a>
<span class="sourceLineNo">073</span>   * collections are generated by {@code factory}.<a name="line.73"></a>
<span class="sourceLineNo">074</span>   *<a name="line.74"></a>
<span class="sourceLineNo">075</span>   * &lt;b&gt;Warning: do not use&lt;/b&gt; this method when the collections returned by<a name="line.75"></a>
<span class="sourceLineNo">076</span>   * {@code factory} implement either {@link List} or {@code Set}! Use the more<a name="line.76"></a>
<span class="sourceLineNo">077</span>   * specific method {@link #newListMultimap}, {@link #newSetMultimap} or {@link<a name="line.77"></a>
<span class="sourceLineNo">078</span>   * #newSortedSetMultimap} instead, to avoid very surprising behavior from<a name="line.78"></a>
<span class="sourceLineNo">079</span>   * {@link Multimap#equals}.<a name="line.79"></a>
<span class="sourceLineNo">080</span>   *<a name="line.80"></a>
<span class="sourceLineNo">081</span>   * &lt;p&gt;The {@code factory}-generated and {@code map} classes determine the<a name="line.81"></a>
<span class="sourceLineNo">082</span>   * multimap iteration order. They also specify the behavior of the<a name="line.82"></a>
<span class="sourceLineNo">083</span>   * {@code equals}, {@code hashCode}, and {@code toString} methods for the<a name="line.83"></a>
<span class="sourceLineNo">084</span>   * multimap and its returned views. However, the multimap's {@code get}<a name="line.84"></a>
<span class="sourceLineNo">085</span>   * method returns instances of a different class than {@code factory.get()}<a name="line.85"></a>
<span class="sourceLineNo">086</span>   * does.<a name="line.86"></a>
<span class="sourceLineNo">087</span>   *<a name="line.87"></a>
<span class="sourceLineNo">088</span>   * &lt;p&gt;The multimap is serializable if {@code map}, {@code factory}, the<a name="line.88"></a>
<span class="sourceLineNo">089</span>   * collections generated by {@code factory}, and the multimap contents are all<a name="line.89"></a>
<span class="sourceLineNo">090</span>   * serializable.<a name="line.90"></a>
<span class="sourceLineNo">091</span>   *<a name="line.91"></a>
<span class="sourceLineNo">092</span>   * &lt;p&gt;The multimap is not threadsafe when any concurrent operations update the<a name="line.92"></a>
<span class="sourceLineNo">093</span>   * multimap, even if {@code map} and the instances generated by<a name="line.93"></a>
<span class="sourceLineNo">094</span>   * {@code factory} are. Concurrent read operations will work correctly. To<a name="line.94"></a>
<span class="sourceLineNo">095</span>   * allow concurrent update operations, wrap the multimap with a call to<a name="line.95"></a>
<span class="sourceLineNo">096</span>   * {@link #synchronizedMultimap}.<a name="line.96"></a>
<span class="sourceLineNo">097</span>   *<a name="line.97"></a>
<span class="sourceLineNo">098</span>   * &lt;p&gt;Call this method only when the simpler methods<a name="line.98"></a>
<span class="sourceLineNo">099</span>   * {@link ArrayListMultimap#create()}, {@link HashMultimap#create()},<a name="line.99"></a>
<span class="sourceLineNo">100</span>   * {@link LinkedHashMultimap#create()}, {@link LinkedListMultimap#create()},<a name="line.100"></a>
<span class="sourceLineNo">101</span>   * {@link TreeMultimap#create()}, and<a name="line.101"></a>
<span class="sourceLineNo">102</span>   * {@link TreeMultimap#create(Comparator, Comparator)} won't suffice.<a name="line.102"></a>
<span class="sourceLineNo">103</span>   *<a name="line.103"></a>
<span class="sourceLineNo">104</span>   * &lt;p&gt;Note: the multimap assumes complete ownership over of {@code map} and<a name="line.104"></a>
<span class="sourceLineNo">105</span>   * the collections returned by {@code factory}. Those objects should not be<a name="line.105"></a>
<span class="sourceLineNo">106</span>   * manually updated and they should not use soft, weak, or phantom references.<a name="line.106"></a>
<span class="sourceLineNo">107</span>   *<a name="line.107"></a>
<span class="sourceLineNo">108</span>   * @param map place to store the mapping from each key to its corresponding<a name="line.108"></a>
<span class="sourceLineNo">109</span>   *     values<a name="line.109"></a>
<span class="sourceLineNo">110</span>   * @param factory supplier of new, empty collections that will each hold all<a name="line.110"></a>
<span class="sourceLineNo">111</span>   *     values for a given key<a name="line.111"></a>
<span class="sourceLineNo">112</span>   * @throws IllegalArgumentException if {@code map} is not empty<a name="line.112"></a>
<span class="sourceLineNo">113</span>   */<a name="line.113"></a>
<span class="sourceLineNo">114</span>  public static &lt;K, V&gt; Multimap&lt;K, V&gt; newMultimap(Map&lt;K, Collection&lt;V&gt;&gt; map,<a name="line.114"></a>
<span class="sourceLineNo">115</span>      final Supplier&lt;? extends Collection&lt;V&gt;&gt; factory) {<a name="line.115"></a>
<span class="sourceLineNo">116</span>    return new CustomMultimap&lt;K, V&gt;(map, factory);<a name="line.116"></a>
<span class="sourceLineNo">117</span>  }<a name="line.117"></a>
<span class="sourceLineNo">118</span><a name="line.118"></a>
<span class="sourceLineNo">119</span>  private static class CustomMultimap&lt;K, V&gt; extends AbstractMultimap&lt;K, V&gt; {<a name="line.119"></a>
<span class="sourceLineNo">120</span>    transient Supplier&lt;? extends Collection&lt;V&gt;&gt; factory;<a name="line.120"></a>
<span class="sourceLineNo">121</span><a name="line.121"></a>
<span class="sourceLineNo">122</span>    CustomMultimap(Map&lt;K, Collection&lt;V&gt;&gt; map,<a name="line.122"></a>
<span class="sourceLineNo">123</span>        Supplier&lt;? extends Collection&lt;V&gt;&gt; factory) {<a name="line.123"></a>
<span class="sourceLineNo">124</span>      super(map);<a name="line.124"></a>
<span class="sourceLineNo">125</span>      this.factory = checkNotNull(factory);<a name="line.125"></a>
<span class="sourceLineNo">126</span>    }<a name="line.126"></a>
<span class="sourceLineNo">127</span><a name="line.127"></a>
<span class="sourceLineNo">128</span>    @Override protected Collection&lt;V&gt; createCollection() {<a name="line.128"></a>
<span class="sourceLineNo">129</span>      return factory.get();<a name="line.129"></a>
<span class="sourceLineNo">130</span>    }<a name="line.130"></a>
<span class="sourceLineNo">131</span><a name="line.131"></a>
<span class="sourceLineNo">132</span>    // can't use Serialization writeMultimap and populateMultimap methods since<a name="line.132"></a>
<span class="sourceLineNo">133</span>    // there's no way to generate the empty backing map.<a name="line.133"></a>
<span class="sourceLineNo">134</span><a name="line.134"></a>
<span class="sourceLineNo">135</span>    /** @serialData the factory and the backing map */<a name="line.135"></a>
<span class="sourceLineNo">136</span>    @GwtIncompatible("java.io.ObjectOutputStream")<a name="line.136"></a>
<span class="sourceLineNo">137</span>    private void writeObject(ObjectOutputStream stream) throws IOException {<a name="line.137"></a>
<span class="sourceLineNo">138</span>      stream.defaultWriteObject();<a name="line.138"></a>
<span class="sourceLineNo">139</span>      stream.writeObject(factory);<a name="line.139"></a>
<span class="sourceLineNo">140</span>      stream.writeObject(backingMap());<a name="line.140"></a>
<span class="sourceLineNo">141</span>    }<a name="line.141"></a>
<span class="sourceLineNo">142</span><a name="line.142"></a>
<span class="sourceLineNo">143</span>    @GwtIncompatible("java.io.ObjectInputStream")<a name="line.143"></a>
<span class="sourceLineNo">144</span>    @SuppressWarnings("unchecked") // reading data stored by writeObject<a name="line.144"></a>
<span class="sourceLineNo">145</span>    private void readObject(ObjectInputStream stream)<a name="line.145"></a>
<span class="sourceLineNo">146</span>        throws IOException, ClassNotFoundException {<a name="line.146"></a>
<span class="sourceLineNo">147</span>      stream.defaultReadObject();<a name="line.147"></a>
<span class="sourceLineNo">148</span>      factory = (Supplier&lt;? extends Collection&lt;V&gt;&gt;) stream.readObject();<a name="line.148"></a>
<span class="sourceLineNo">149</span>      Map&lt;K, Collection&lt;V&gt;&gt; map = (Map&lt;K, Collection&lt;V&gt;&gt;) stream.readObject();<a name="line.149"></a>
<span class="sourceLineNo">150</span>      setMap(map);<a name="line.150"></a>
<span class="sourceLineNo">151</span>    }<a name="line.151"></a>
<span class="sourceLineNo">152</span><a name="line.152"></a>
<span class="sourceLineNo">153</span>    @GwtIncompatible("java serialization not supported")<a name="line.153"></a>
<span class="sourceLineNo">154</span>    private static final long serialVersionUID = 0;<a name="line.154"></a>
<span class="sourceLineNo">155</span>  }<a name="line.155"></a>
<span class="sourceLineNo">156</span><a name="line.156"></a>
<span class="sourceLineNo">157</span>  /**<a name="line.157"></a>
<span class="sourceLineNo">158</span>   * Creates a new {@code ListMultimap} that uses the provided map and factory.<a name="line.158"></a>
<span class="sourceLineNo">159</span>   * It can generate a multimap based on arbitrary {@link Map} and {@link List}<a name="line.159"></a>
<span class="sourceLineNo">160</span>   * classes.<a name="line.160"></a>
<span class="sourceLineNo">161</span>   *<a name="line.161"></a>
<span class="sourceLineNo">162</span>   * &lt;p&gt;The {@code factory}-generated and {@code map} classes determine the<a name="line.162"></a>
<span class="sourceLineNo">163</span>   * multimap iteration order. They also specify the behavior of the<a name="line.163"></a>
<span class="sourceLineNo">164</span>   * {@code equals}, {@code hashCode}, and {@code toString} methods for the<a name="line.164"></a>
<span class="sourceLineNo">165</span>   * multimap and its returned views. The multimap's {@code get}, {@code<a name="line.165"></a>
<span class="sourceLineNo">166</span>   * removeAll}, and {@code replaceValues} methods return {@code RandomAccess}<a name="line.166"></a>
<span class="sourceLineNo">167</span>   * lists if the factory does. However, the multimap's {@code get} method<a name="line.167"></a>
<span class="sourceLineNo">168</span>   * returns instances of a different class than does {@code factory.get()}.<a name="line.168"></a>
<span class="sourceLineNo">169</span>   *<a name="line.169"></a>
<span class="sourceLineNo">170</span>   * &lt;p&gt;The multimap is serializable if {@code map}, {@code factory}, the<a name="line.170"></a>
<span class="sourceLineNo">171</span>   * lists generated by {@code factory}, and the multimap contents are all<a name="line.171"></a>
<span class="sourceLineNo">172</span>   * serializable.<a name="line.172"></a>
<span class="sourceLineNo">173</span>   *<a name="line.173"></a>
<span class="sourceLineNo">174</span>   * &lt;p&gt;The multimap is not threadsafe when any concurrent operations update the<a name="line.174"></a>
<span class="sourceLineNo">175</span>   * multimap, even if {@code map} and the instances generated by<a name="line.175"></a>
<span class="sourceLineNo">176</span>   * {@code factory} are. Concurrent read operations will work correctly. To<a name="line.176"></a>
<span class="sourceLineNo">177</span>   * allow concurrent update operations, wrap the multimap with a call to<a name="line.177"></a>
<span class="sourceLineNo">178</span>   * {@link #synchronizedListMultimap}.<a name="line.178"></a>
<span class="sourceLineNo">179</span>   *<a name="line.179"></a>
<span class="sourceLineNo">180</span>   * &lt;p&gt;Call this method only when the simpler methods<a name="line.180"></a>
<span class="sourceLineNo">181</span>   * {@link ArrayListMultimap#create()} and {@link LinkedListMultimap#create()}<a name="line.181"></a>
<span class="sourceLineNo">182</span>   * won't suffice.<a name="line.182"></a>
<span class="sourceLineNo">183</span>   *<a name="line.183"></a>
<span class="sourceLineNo">184</span>   * &lt;p&gt;Note: the multimap assumes complete ownership over of {@code map} and<a name="line.184"></a>
<span class="sourceLineNo">185</span>   * the lists returned by {@code factory}. Those objects should not be manually<a name="line.185"></a>
<span class="sourceLineNo">186</span>   * updated, they should be empty when provided, and they should not use soft,<a name="line.186"></a>
<span class="sourceLineNo">187</span>   * weak, or phantom references.<a name="line.187"></a>
<span class="sourceLineNo">188</span>   *<a name="line.188"></a>
<span class="sourceLineNo">189</span>   * @param map place to store the mapping from each key to its corresponding<a name="line.189"></a>
<span class="sourceLineNo">190</span>   *     values<a name="line.190"></a>
<span class="sourceLineNo">191</span>   * @param factory supplier of new, empty lists that will each hold all values<a name="line.191"></a>
<span class="sourceLineNo">192</span>   *     for a given key<a name="line.192"></a>
<span class="sourceLineNo">193</span>   * @throws IllegalArgumentException if {@code map} is not empty<a name="line.193"></a>
<span class="sourceLineNo">194</span>   */<a name="line.194"></a>
<span class="sourceLineNo">195</span>  public static &lt;K, V&gt; ListMultimap&lt;K, V&gt; newListMultimap(<a name="line.195"></a>
<span class="sourceLineNo">196</span>      Map&lt;K, Collection&lt;V&gt;&gt; map, final Supplier&lt;? extends List&lt;V&gt;&gt; factory) {<a name="line.196"></a>
<span class="sourceLineNo">197</span>    return new CustomListMultimap&lt;K, V&gt;(map, factory);<a name="line.197"></a>
<span class="sourceLineNo">198</span>  }<a name="line.198"></a>
<span class="sourceLineNo">199</span><a name="line.199"></a>
<span class="sourceLineNo">200</span>  private static class CustomListMultimap&lt;K, V&gt;<a name="line.200"></a>
<span class="sourceLineNo">201</span>      extends AbstractListMultimap&lt;K, V&gt; {<a name="line.201"></a>
<span class="sourceLineNo">202</span>    transient Supplier&lt;? extends List&lt;V&gt;&gt; factory;<a name="line.202"></a>
<span class="sourceLineNo">203</span><a name="line.203"></a>
<span class="sourceLineNo">204</span>    CustomListMultimap(Map&lt;K, Collection&lt;V&gt;&gt; map,<a name="line.204"></a>
<span class="sourceLineNo">205</span>        Supplier&lt;? extends List&lt;V&gt;&gt; factory) {<a name="line.205"></a>
<span class="sourceLineNo">206</span>      super(map);<a name="line.206"></a>
<span class="sourceLineNo">207</span>      this.factory = checkNotNull(factory);<a name="line.207"></a>
<span class="sourceLineNo">208</span>    }<a name="line.208"></a>
<span class="sourceLineNo">209</span><a name="line.209"></a>
<span class="sourceLineNo">210</span>    @Override protected List&lt;V&gt; createCollection() {<a name="line.210"></a>
<span class="sourceLineNo">211</span>      return factory.get();<a name="line.211"></a>
<span class="sourceLineNo">212</span>    }<a name="line.212"></a>
<span class="sourceLineNo">213</span><a name="line.213"></a>
<span class="sourceLineNo">214</span>    /** @serialData the factory and the backing map */<a name="line.214"></a>
<span class="sourceLineNo">215</span>    @GwtIncompatible("java.io.ObjectOutputStream")<a name="line.215"></a>
<span class="sourceLineNo">216</span>    private void writeObject(ObjectOutputStream stream) throws IOException {<a name="line.216"></a>
<span class="sourceLineNo">217</span>      stream.defaultWriteObject();<a name="line.217"></a>
<span class="sourceLineNo">218</span>      stream.writeObject(factory);<a name="line.218"></a>
<span class="sourceLineNo">219</span>      stream.writeObject(backingMap());<a name="line.219"></a>
<span class="sourceLineNo">220</span>    }<a name="line.220"></a>
<span class="sourceLineNo">221</span><a name="line.221"></a>
<span class="sourceLineNo">222</span>    @GwtIncompatible("java.io.ObjectInputStream")<a name="line.222"></a>
<span class="sourceLineNo">223</span>    @SuppressWarnings("unchecked") // reading data stored by writeObject<a name="line.223"></a>
<span class="sourceLineNo">224</span>    private void readObject(ObjectInputStream stream)<a name="line.224"></a>
<span class="sourceLineNo">225</span>        throws IOException, ClassNotFoundException {<a name="line.225"></a>
<span class="sourceLineNo">226</span>      stream.defaultReadObject();<a name="line.226"></a>
<span class="sourceLineNo">227</span>      factory = (Supplier&lt;? extends List&lt;V&gt;&gt;) stream.readObject();<a name="line.227"></a>
<span class="sourceLineNo">228</span>      Map&lt;K, Collection&lt;V&gt;&gt; map = (Map&lt;K, Collection&lt;V&gt;&gt;) stream.readObject();<a name="line.228"></a>
<span class="sourceLineNo">229</span>      setMap(map);<a name="line.229"></a>
<span class="sourceLineNo">230</span>    }<a name="line.230"></a>
<span class="sourceLineNo">231</span><a name="line.231"></a>
<span class="sourceLineNo">232</span>    @GwtIncompatible("java serialization not supported")<a name="line.232"></a>
<span class="sourceLineNo">233</span>    private static final long serialVersionUID = 0;<a name="line.233"></a>
<span class="sourceLineNo">234</span>  }<a name="line.234"></a>
<span class="sourceLineNo">235</span><a name="line.235"></a>
<span class="sourceLineNo">236</span>  /**<a name="line.236"></a>
<span class="sourceLineNo">237</span>   * Creates a new {@code SetMultimap} that uses the provided map and factory.<a name="line.237"></a>
<span class="sourceLineNo">238</span>   * It can generate a multimap based on arbitrary {@link Map} and {@link Set}<a name="line.238"></a>
<span class="sourceLineNo">239</span>   * classes.<a name="line.239"></a>
<span class="sourceLineNo">240</span>   *<a name="line.240"></a>
<span class="sourceLineNo">241</span>   * &lt;p&gt;The {@code factory}-generated and {@code map} classes determine the<a name="line.241"></a>
<span class="sourceLineNo">242</span>   * multimap iteration order. They also specify the behavior of the<a name="line.242"></a>
<span class="sourceLineNo">243</span>   * {@code equals}, {@code hashCode}, and {@code toString} methods for the<a name="line.243"></a>
<span class="sourceLineNo">244</span>   * multimap and its returned views. However, the multimap's {@code get}<a name="line.244"></a>
<span class="sourceLineNo">245</span>   * method returns instances of a different class than {@code factory.get()}<a name="line.245"></a>
<span class="sourceLineNo">246</span>   * does.<a name="line.246"></a>
<span class="sourceLineNo">247</span>   *<a name="line.247"></a>
<span class="sourceLineNo">248</span>   * &lt;p&gt;The multimap is serializable if {@code map}, {@code factory}, the<a name="line.248"></a>
<span class="sourceLineNo">249</span>   * sets generated by {@code factory}, and the multimap contents are all<a name="line.249"></a>
<span class="sourceLineNo">250</span>   * serializable.<a name="line.250"></a>
<span class="sourceLineNo">251</span>   *<a name="line.251"></a>
<span class="sourceLineNo">252</span>   * &lt;p&gt;The multimap is not threadsafe when any concurrent operations update the<a name="line.252"></a>
<span class="sourceLineNo">253</span>   * multimap, even if {@code map} and the instances generated by<a name="line.253"></a>
<span class="sourceLineNo">254</span>   * {@code factory} are. Concurrent read operations will work correctly. To<a name="line.254"></a>
<span class="sourceLineNo">255</span>   * allow concurrent update operations, wrap the multimap with a call to<a name="line.255"></a>
<span class="sourceLineNo">256</span>   * {@link #synchronizedSetMultimap}.<a name="line.256"></a>
<span class="sourceLineNo">257</span>   *<a name="line.257"></a>
<span class="sourceLineNo">258</span>   * &lt;p&gt;Call this method only when the simpler methods<a name="line.258"></a>
<span class="sourceLineNo">259</span>   * {@link HashMultimap#create()}, {@link LinkedHashMultimap#create()},<a name="line.259"></a>
<span class="sourceLineNo">260</span>   * {@link TreeMultimap#create()}, and<a name="line.260"></a>
<span class="sourceLineNo">261</span>   * {@link TreeMultimap#create(Comparator, Comparator)} won't suffice.<a name="line.261"></a>
<span class="sourceLineNo">262</span>   *<a name="line.262"></a>
<span class="sourceLineNo">263</span>   * &lt;p&gt;Note: the multimap assumes complete ownership over of {@code map} and<a name="line.263"></a>
<span class="sourceLineNo">264</span>   * the sets returned by {@code factory}. Those objects should not be manually<a name="line.264"></a>
<span class="sourceLineNo">265</span>   * updated and they should not use soft, weak, or phantom references.<a name="line.265"></a>
<span class="sourceLineNo">266</span>   *<a name="line.266"></a>
<span class="sourceLineNo">267</span>   * @param map place to store the mapping from each key to its corresponding<a name="line.267"></a>
<span class="sourceLineNo">268</span>   *     values<a name="line.268"></a>
<span class="sourceLineNo">269</span>   * @param factory supplier of new, empty sets that will each hold all values<a name="line.269"></a>
<span class="sourceLineNo">270</span>   *     for a given key<a name="line.270"></a>
<span class="sourceLineNo">271</span>   * @throws IllegalArgumentException if {@code map} is not empty<a name="line.271"></a>
<span class="sourceLineNo">272</span>   */<a name="line.272"></a>
<span class="sourceLineNo">273</span>  public static &lt;K, V&gt; SetMultimap&lt;K, V&gt; newSetMultimap(<a name="line.273"></a>
<span class="sourceLineNo">274</span>      Map&lt;K, Collection&lt;V&gt;&gt; map, final Supplier&lt;? extends Set&lt;V&gt;&gt; factory) {<a name="line.274"></a>
<span class="sourceLineNo">275</span>    return new CustomSetMultimap&lt;K, V&gt;(map, factory);<a name="line.275"></a>
<span class="sourceLineNo">276</span>  }<a name="line.276"></a>
<span class="sourceLineNo">277</span><a name="line.277"></a>
<span class="sourceLineNo">278</span>  private static class CustomSetMultimap&lt;K, V&gt;<a name="line.278"></a>
<span class="sourceLineNo">279</span>      extends AbstractSetMultimap&lt;K, V&gt; {<a name="line.279"></a>
<span class="sourceLineNo">280</span>    transient Supplier&lt;? extends Set&lt;V&gt;&gt; factory;<a name="line.280"></a>
<span class="sourceLineNo">281</span><a name="line.281"></a>
<span class="sourceLineNo">282</span>    CustomSetMultimap(Map&lt;K, Collection&lt;V&gt;&gt; map,<a name="line.282"></a>
<span class="sourceLineNo">283</span>        Supplier&lt;? extends Set&lt;V&gt;&gt; factory) {<a name="line.283"></a>
<span class="sourceLineNo">284</span>      super(map);<a name="line.284"></a>
<span class="sourceLineNo">285</span>      this.factory = checkNotNull(factory);<a name="line.285"></a>
<span class="sourceLineNo">286</span>    }<a name="line.286"></a>
<span class="sourceLineNo">287</span><a name="line.287"></a>
<span class="sourceLineNo">288</span>    @Override protected Set&lt;V&gt; createCollection() {<a name="line.288"></a>
<span class="sourceLineNo">289</span>      return factory.get();<a name="line.289"></a>
<span class="sourceLineNo">290</span>    }<a name="line.290"></a>
<span class="sourceLineNo">291</span><a name="line.291"></a>
<span class="sourceLineNo">292</span>    /** @serialData the factory and the backing map */<a name="line.292"></a>
<span class="sourceLineNo">293</span>    @GwtIncompatible("java.io.ObjectOutputStream")<a name="line.293"></a>
<span class="sourceLineNo">294</span>    private void writeObject(ObjectOutputStream stream) throws IOException {<a name="line.294"></a>
<span class="sourceLineNo">295</span>      stream.defaultWriteObject();<a name="line.295"></a>
<span class="sourceLineNo">296</span>      stream.writeObject(factory);<a name="line.296"></a>
<span class="sourceLineNo">297</span>      stream.writeObject(backingMap());<a name="line.297"></a>
<span class="sourceLineNo">298</span>    }<a name="line.298"></a>
<span class="sourceLineNo">299</span><a name="line.299"></a>
<span class="sourceLineNo">300</span>    @GwtIncompatible("java.io.ObjectInputStream")<a name="line.300"></a>
<span class="sourceLineNo">301</span>    @SuppressWarnings("unchecked") // reading data stored by writeObject<a name="line.301"></a>
<span class="sourceLineNo">302</span>    private void readObject(ObjectInputStream stream)<a name="line.302"></a>
<span class="sourceLineNo">303</span>        throws IOException, ClassNotFoundException {<a name="line.303"></a>
<span class="sourceLineNo">304</span>      stream.defaultReadObject();<a name="line.304"></a>
<span class="sourceLineNo">305</span>      factory = (Supplier&lt;? extends Set&lt;V&gt;&gt;) stream.readObject();<a name="line.305"></a>
<span class="sourceLineNo">306</span>      Map&lt;K, Collection&lt;V&gt;&gt; map = (Map&lt;K, Collection&lt;V&gt;&gt;) stream.readObject();<a name="line.306"></a>
<span class="sourceLineNo">307</span>      setMap(map);<a name="line.307"></a>
<span class="sourceLineNo">308</span>    }<a name="line.308"></a>
<span class="sourceLineNo">309</span><a name="line.309"></a>
<span class="sourceLineNo">310</span>    @GwtIncompatible("not needed in emulated source")<a name="line.310"></a>
<span class="sourceLineNo">311</span>    private static final long serialVersionUID = 0;<a name="line.311"></a>
<span class="sourceLineNo">312</span>  }<a name="line.312"></a>
<span class="sourceLineNo">313</span><a name="line.313"></a>
<span class="sourceLineNo">314</span>  /**<a name="line.314"></a>
<span class="sourceLineNo">315</span>   * Creates a new {@code SortedSetMultimap} that uses the provided map and<a name="line.315"></a>
<span class="sourceLineNo">316</span>   * factory. It can generate a multimap based on arbitrary {@link Map} and<a name="line.316"></a>
<span class="sourceLineNo">317</span>   * {@link SortedSet} classes.<a name="line.317"></a>
<span class="sourceLineNo">318</span>   *<a name="line.318"></a>
<span class="sourceLineNo">319</span>   * &lt;p&gt;The {@code factory}-generated and {@code map} classes determine the<a name="line.319"></a>
<span class="sourceLineNo">320</span>   * multimap iteration order. They also specify the behavior of the<a name="line.320"></a>
<span class="sourceLineNo">321</span>   * {@code equals}, {@code hashCode}, and {@code toString} methods for the<a name="line.321"></a>
<span class="sourceLineNo">322</span>   * multimap and its returned views. However, the multimap's {@code get}<a name="line.322"></a>
<span class="sourceLineNo">323</span>   * method returns instances of a different class than {@code factory.get()}<a name="line.323"></a>
<span class="sourceLineNo">324</span>   * does.<a name="line.324"></a>
<span class="sourceLineNo">325</span>   *<a name="line.325"></a>
<span class="sourceLineNo">326</span>   * &lt;p&gt;The multimap is serializable if {@code map}, {@code factory}, the<a name="line.326"></a>
<span class="sourceLineNo">327</span>   * sets generated by {@code factory}, and the multimap contents are all<a name="line.327"></a>
<span class="sourceLineNo">328</span>   * serializable.<a name="line.328"></a>
<span class="sourceLineNo">329</span>   *<a name="line.329"></a>
<span class="sourceLineNo">330</span>   * &lt;p&gt;The multimap is not threadsafe when any concurrent operations update the<a name="line.330"></a>
<span class="sourceLineNo">331</span>   * multimap, even if {@code map} and the instances generated by<a name="line.331"></a>
<span class="sourceLineNo">332</span>   * {@code factory} are. Concurrent read operations will work correctly. To<a name="line.332"></a>
<span class="sourceLineNo">333</span>   * allow concurrent update operations, wrap the multimap with a call to<a name="line.333"></a>
<span class="sourceLineNo">334</span>   * {@link #synchronizedSortedSetMultimap}.<a name="line.334"></a>
<span class="sourceLineNo">335</span>   *<a name="line.335"></a>
<span class="sourceLineNo">336</span>   * &lt;p&gt;Call this method only when the simpler methods<a name="line.336"></a>
<span class="sourceLineNo">337</span>   * {@link TreeMultimap#create()} and<a name="line.337"></a>
<span class="sourceLineNo">338</span>   * {@link TreeMultimap#create(Comparator, Comparator)} won't suffice.<a name="line.338"></a>
<span class="sourceLineNo">339</span>   *<a name="line.339"></a>
<span class="sourceLineNo">340</span>   * &lt;p&gt;Note: the multimap assumes complete ownership over of {@code map} and<a name="line.340"></a>
<span class="sourceLineNo">341</span>   * the sets returned by {@code factory}. Those objects should not be manually<a name="line.341"></a>
<span class="sourceLineNo">342</span>   * updated and they should not use soft, weak, or phantom references.<a name="line.342"></a>
<span class="sourceLineNo">343</span>   *<a name="line.343"></a>
<span class="sourceLineNo">344</span>   * @param map place to store the mapping from each key to its corresponding<a name="line.344"></a>
<span class="sourceLineNo">345</span>   *     values<a name="line.345"></a>
<span class="sourceLineNo">346</span>   * @param factory supplier of new, empty sorted sets that will each hold<a name="line.346"></a>
<span class="sourceLineNo">347</span>   *     all values for a given key<a name="line.347"></a>
<span class="sourceLineNo">348</span>   * @throws IllegalArgumentException if {@code map} is not empty<a name="line.348"></a>
<span class="sourceLineNo">349</span>   */<a name="line.349"></a>
<span class="sourceLineNo">350</span>  public static &lt;K, V&gt; SortedSetMultimap&lt;K, V&gt; newSortedSetMultimap(<a name="line.350"></a>
<span class="sourceLineNo">351</span>      Map&lt;K, Collection&lt;V&gt;&gt; map,<a name="line.351"></a>
<span class="sourceLineNo">352</span>      final Supplier&lt;? extends SortedSet&lt;V&gt;&gt; factory) {<a name="line.352"></a>
<span class="sourceLineNo">353</span>    return new CustomSortedSetMultimap&lt;K, V&gt;(map, factory);<a name="line.353"></a>
<span class="sourceLineNo">354</span>  }<a name="line.354"></a>
<span class="sourceLineNo">355</span><a name="line.355"></a>
<span class="sourceLineNo">356</span>  private static class CustomSortedSetMultimap&lt;K, V&gt;<a name="line.356"></a>
<span class="sourceLineNo">357</span>      extends AbstractSortedSetMultimap&lt;K, V&gt; {<a name="line.357"></a>
<span class="sourceLineNo">358</span>    transient Supplier&lt;? extends SortedSet&lt;V&gt;&gt; factory;<a name="line.358"></a>
<span class="sourceLineNo">359</span>    transient Comparator&lt;? super V&gt; valueComparator;<a name="line.359"></a>
<span class="sourceLineNo">360</span><a name="line.360"></a>
<span class="sourceLineNo">361</span>    CustomSortedSetMultimap(Map&lt;K, Collection&lt;V&gt;&gt; map,<a name="line.361"></a>
<span class="sourceLineNo">362</span>        Supplier&lt;? extends SortedSet&lt;V&gt;&gt; factory) {<a name="line.362"></a>
<span class="sourceLineNo">363</span>      super(map);<a name="line.363"></a>
<span class="sourceLineNo">364</span>      this.factory = checkNotNull(factory);<a name="line.364"></a>
<span class="sourceLineNo">365</span>      valueComparator = factory.get().comparator();<a name="line.365"></a>
<span class="sourceLineNo">366</span>    }<a name="line.366"></a>
<span class="sourceLineNo">367</span><a name="line.367"></a>
<span class="sourceLineNo">368</span>    @Override protected SortedSet&lt;V&gt; createCollection() {<a name="line.368"></a>
<span class="sourceLineNo">369</span>      return factory.get();<a name="line.369"></a>
<span class="sourceLineNo">370</span>    }<a name="line.370"></a>
<span class="sourceLineNo">371</span><a name="line.371"></a>
<span class="sourceLineNo">372</span>    @Override public Comparator&lt;? super V&gt; valueComparator() {<a name="line.372"></a>
<span class="sourceLineNo">373</span>      return valueComparator;<a name="line.373"></a>
<span class="sourceLineNo">374</span>    }<a name="line.374"></a>
<span class="sourceLineNo">375</span><a name="line.375"></a>
<span class="sourceLineNo">376</span>    /** @serialData the factory and the backing map */<a name="line.376"></a>
<span class="sourceLineNo">377</span>    @GwtIncompatible("java.io.ObjectOutputStream")<a name="line.377"></a>
<span class="sourceLineNo">378</span>    private void writeObject(ObjectOutputStream stream) throws IOException {<a name="line.378"></a>
<span class="sourceLineNo">379</span>      stream.defaultWriteObject();<a name="line.379"></a>
<span class="sourceLineNo">380</span>      stream.writeObject(factory);<a name="line.380"></a>
<span class="sourceLineNo">381</span>      stream.writeObject(backingMap());<a name="line.381"></a>
<span class="sourceLineNo">382</span>    }<a name="line.382"></a>
<span class="sourceLineNo">383</span><a name="line.383"></a>
<span class="sourceLineNo">384</span>    @GwtIncompatible("java.io.ObjectInputStream")<a name="line.384"></a>
<span class="sourceLineNo">385</span>    @SuppressWarnings("unchecked") // reading data stored by writeObject<a name="line.385"></a>
<span class="sourceLineNo">386</span>    private void readObject(ObjectInputStream stream)<a name="line.386"></a>
<span class="sourceLineNo">387</span>        throws IOException, ClassNotFoundException {<a name="line.387"></a>
<span class="sourceLineNo">388</span>      stream.defaultReadObject();<a name="line.388"></a>
<span class="sourceLineNo">389</span>      factory = (Supplier&lt;? extends SortedSet&lt;V&gt;&gt;) stream.readObject();<a name="line.389"></a>
<span class="sourceLineNo">390</span>      valueComparator = factory.get().comparator();<a name="line.390"></a>
<span class="sourceLineNo">391</span>      Map&lt;K, Collection&lt;V&gt;&gt; map = (Map&lt;K, Collection&lt;V&gt;&gt;) stream.readObject();<a name="line.391"></a>
<span class="sourceLineNo">392</span>      setMap(map);<a name="line.392"></a>
<span class="sourceLineNo">393</span>    }<a name="line.393"></a>
<span class="sourceLineNo">394</span><a name="line.394"></a>
<span class="sourceLineNo">395</span>    @GwtIncompatible("not needed in emulated source")<a name="line.395"></a>
<span class="sourceLineNo">396</span>    private static final long serialVersionUID = 0;<a name="line.396"></a>
<span class="sourceLineNo">397</span>  }<a name="line.397"></a>
<span class="sourceLineNo">398</span><a name="line.398"></a>
<span class="sourceLineNo">399</span>  /**<a name="line.399"></a>
<span class="sourceLineNo">400</span>   * Copies each key-value mapping in {@code source} into {@code dest}, with<a name="line.400"></a>
<span class="sourceLineNo">401</span>   * its key and value reversed.<a name="line.401"></a>
<span class="sourceLineNo">402</span>   *<a name="line.402"></a>
<span class="sourceLineNo">403</span>   * &lt;p&gt;If {@code source} is an {@link ImmutableMultimap}, consider using<a name="line.403"></a>
<span class="sourceLineNo">404</span>   * {@link ImmutableMultimap#inverse} instead.<a name="line.404"></a>
<span class="sourceLineNo">405</span>   *<a name="line.405"></a>
<span class="sourceLineNo">406</span>   * @param source any multimap<a name="line.406"></a>
<span class="sourceLineNo">407</span>   * @param dest the multimap to copy into; usually empty<a name="line.407"></a>
<span class="sourceLineNo">408</span>   * @return {@code dest}<a name="line.408"></a>
<span class="sourceLineNo">409</span>   */<a name="line.409"></a>
<span class="sourceLineNo">410</span>  public static &lt;K, V, M extends Multimap&lt;K, V&gt;&gt; M invertFrom(<a name="line.410"></a>
<span class="sourceLineNo">411</span>      Multimap&lt;? extends V, ? extends K&gt; source, M dest) {<a name="line.411"></a>
<span class="sourceLineNo">412</span>    checkNotNull(dest);<a name="line.412"></a>
<span class="sourceLineNo">413</span>    for (Map.Entry&lt;? extends V, ? extends K&gt; entry : source.entries()) {<a name="line.413"></a>
<span class="sourceLineNo">414</span>      dest.put(entry.getValue(), entry.getKey());<a name="line.414"></a>
<span class="sourceLineNo">415</span>    }<a name="line.415"></a>
<span class="sourceLineNo">416</span>    return dest;<a name="line.416"></a>
<span class="sourceLineNo">417</span>  }<a name="line.417"></a>
<span class="sourceLineNo">418</span><a name="line.418"></a>
<span class="sourceLineNo">419</span>  /**<a name="line.419"></a>
<span class="sourceLineNo">420</span>   * Returns a synchronized (thread-safe) multimap backed by the specified<a name="line.420"></a>
<span class="sourceLineNo">421</span>   * multimap. In order to guarantee serial access, it is critical that<a name="line.421"></a>
<span class="sourceLineNo">422</span>   * &lt;b&gt;all&lt;/b&gt; access to the backing multimap is accomplished through the<a name="line.422"></a>
<span class="sourceLineNo">423</span>   * returned multimap.<a name="line.423"></a>
<span class="sourceLineNo">424</span>   *<a name="line.424"></a>
<span class="sourceLineNo">425</span>   * &lt;p&gt;It is imperative that the user manually synchronize on the returned<a name="line.425"></a>
<span class="sourceLineNo">426</span>   * multimap when accessing any of its collection views: &lt;pre&gt;   {@code<a name="line.426"></a>
<span class="sourceLineNo">427</span>   *<a name="line.427"></a>
<span class="sourceLineNo">428</span>   *   Multimap&lt;K, V&gt; multimap = Multimaps.synchronizedMultimap(<a name="line.428"></a>
<span class="sourceLineNo">429</span>   *       HashMultimap.&lt;K, V&gt;create());<a name="line.429"></a>
<span class="sourceLineNo">430</span>   *   ...<a name="line.430"></a>
<span class="sourceLineNo">431</span>   *   Collection&lt;V&gt; values = multimap.get(key);  // Needn't be in synchronized block<a name="line.431"></a>
<span class="sourceLineNo">432</span>   *   ...<a name="line.432"></a>
<span class="sourceLineNo">433</span>   *   synchronized (multimap) {  // Synchronizing on multimap, not values!<a name="line.433"></a>
<span class="sourceLineNo">434</span>   *     Iterator&lt;V&gt; i = values.iterator(); // Must be in synchronized block<a name="line.434"></a>
<span class="sourceLineNo">435</span>   *     while (i.hasNext()) {<a name="line.435"></a>
<span class="sourceLineNo">436</span>   *       foo(i.next());<a name="line.436"></a>
<span class="sourceLineNo">437</span>   *     }<a name="line.437"></a>
<span class="sourceLineNo">438</span>   *   }}&lt;/pre&gt;<a name="line.438"></a>
<span class="sourceLineNo">439</span>   *<a name="line.439"></a>
<span class="sourceLineNo">440</span>   * Failure to follow this advice may result in non-deterministic behavior.<a name="line.440"></a>
<span class="sourceLineNo">441</span>   *<a name="line.441"></a>
<span class="sourceLineNo">442</span>   * &lt;p&gt;Note that the generated multimap's {@link Multimap#removeAll} and<a name="line.442"></a>
<span class="sourceLineNo">443</span>   * {@link Multimap#replaceValues} methods return collections that aren't<a name="line.443"></a>
<span class="sourceLineNo">444</span>   * synchronized.<a name="line.444"></a>
<span class="sourceLineNo">445</span>   *<a name="line.445"></a>
<span class="sourceLineNo">446</span>   * &lt;p&gt;The returned multimap will be serializable if the specified multimap is<a name="line.446"></a>
<span class="sourceLineNo">447</span>   * serializable.<a name="line.447"></a>
<span class="sourceLineNo">448</span>   *<a name="line.448"></a>
<span class="sourceLineNo">449</span>   * @param multimap the multimap to be wrapped in a synchronized view<a name="line.449"></a>
<span class="sourceLineNo">450</span>   * @return a synchronized view of the specified multimap<a name="line.450"></a>
<span class="sourceLineNo">451</span>   */<a name="line.451"></a>
<span class="sourceLineNo">452</span>  public static &lt;K, V&gt; Multimap&lt;K, V&gt; synchronizedMultimap(<a name="line.452"></a>
<span class="sourceLineNo">453</span>      Multimap&lt;K, V&gt; multimap) {<a name="line.453"></a>
<span class="sourceLineNo">454</span>    return Synchronized.multimap(multimap, null);<a name="line.454"></a>
<span class="sourceLineNo">455</span>  }<a name="line.455"></a>
<span class="sourceLineNo">456</span><a name="line.456"></a>
<span class="sourceLineNo">457</span>  /**<a name="line.457"></a>
<span class="sourceLineNo">458</span>   * Returns an unmodifiable view of the specified multimap. Query operations on<a name="line.458"></a>
<span class="sourceLineNo">459</span>   * the returned multimap "read through" to the specified multimap, and<a name="line.459"></a>
<span class="sourceLineNo">460</span>   * attempts to modify the returned multimap, either directly or through the<a name="line.460"></a>
<span class="sourceLineNo">461</span>   * multimap's views, result in an {@code UnsupportedOperationException}.<a name="line.461"></a>
<span class="sourceLineNo">462</span>   *<a name="line.462"></a>
<span class="sourceLineNo">463</span>   * &lt;p&gt;Note that the generated multimap's {@link Multimap#removeAll} and<a name="line.463"></a>
<span class="sourceLineNo">464</span>   * {@link Multimap#replaceValues} methods return collections that are<a name="line.464"></a>
<span class="sourceLineNo">465</span>   * modifiable.<a name="line.465"></a>
<span class="sourceLineNo">466</span>   *<a name="line.466"></a>
<span class="sourceLineNo">467</span>   * &lt;p&gt;The returned multimap will be serializable if the specified multimap is<a name="line.467"></a>
<span class="sourceLineNo">468</span>   * serializable.<a name="line.468"></a>
<span class="sourceLineNo">469</span>   *<a name="line.469"></a>
<span class="sourceLineNo">470</span>   * @param delegate the multimap for which an unmodifiable view is to be<a name="line.470"></a>
<span class="sourceLineNo">471</span>   *     returned<a name="line.471"></a>
<span class="sourceLineNo">472</span>   * @return an unmodifiable view of the specified multimap<a name="line.472"></a>
<span class="sourceLineNo">473</span>   */<a name="line.473"></a>
<span class="sourceLineNo">474</span>  public static &lt;K, V&gt; Multimap&lt;K, V&gt; unmodifiableMultimap(<a name="line.474"></a>
<span class="sourceLineNo">475</span>      Multimap&lt;K, V&gt; delegate) {<a name="line.475"></a>
<span class="sourceLineNo">476</span>    if (delegate instanceof UnmodifiableMultimap ||<a name="line.476"></a>
<span class="sourceLineNo">477</span>        delegate instanceof ImmutableMultimap) {<a name="line.477"></a>
<span class="sourceLineNo">478</span>      return delegate;<a name="line.478"></a>
<span class="sourceLineNo">479</span>    }<a name="line.479"></a>
<span class="sourceLineNo">480</span>    return new UnmodifiableMultimap&lt;K, V&gt;(delegate);<a name="line.480"></a>
<span class="sourceLineNo">481</span>  }<a name="line.481"></a>
<span class="sourceLineNo">482</span><a name="line.482"></a>
<span class="sourceLineNo">483</span>  /**<a name="line.483"></a>
<span class="sourceLineNo">484</span>   * Simply returns its argument.<a name="line.484"></a>
<span class="sourceLineNo">485</span>   *<a name="line.485"></a>
<span class="sourceLineNo">486</span>   * @deprecated no need to use this<a name="line.486"></a>
<span class="sourceLineNo">487</span>   * @since 10.0<a name="line.487"></a>
<span class="sourceLineNo">488</span>   */<a name="line.488"></a>
<span class="sourceLineNo">489</span>  @Deprecated public static &lt;K, V&gt; Multimap&lt;K, V&gt; unmodifiableMultimap(<a name="line.489"></a>
<span class="sourceLineNo">490</span>      ImmutableMultimap&lt;K, V&gt; delegate) {<a name="line.490"></a>
<span class="sourceLineNo">491</span>    return checkNotNull(delegate);<a name="line.491"></a>
<span class="sourceLineNo">492</span>  }<a name="line.492"></a>
<span class="sourceLineNo">493</span><a name="line.493"></a>
<span class="sourceLineNo">494</span>  private static class UnmodifiableMultimap&lt;K, V&gt;<a name="line.494"></a>
<span class="sourceLineNo">495</span>      extends ForwardingMultimap&lt;K, V&gt; implements Serializable {<a name="line.495"></a>
<span class="sourceLineNo">496</span>    final Multimap&lt;K, V&gt; delegate;<a name="line.496"></a>
<span class="sourceLineNo">497</span>    transient Collection&lt;Entry&lt;K, V&gt;&gt; entries;<a name="line.497"></a>
<span class="sourceLineNo">498</span>    transient Multiset&lt;K&gt; keys;<a name="line.498"></a>
<span class="sourceLineNo">499</span>    transient Set&lt;K&gt; keySet;<a name="line.499"></a>
<span class="sourceLineNo">500</span>    transient Collection&lt;V&gt; values;<a name="line.500"></a>
<span class="sourceLineNo">501</span>    transient Map&lt;K, Collection&lt;V&gt;&gt; map;<a name="line.501"></a>
<span class="sourceLineNo">502</span><a name="line.502"></a>
<span class="sourceLineNo">503</span>    UnmodifiableMultimap(final Multimap&lt;K, V&gt; delegate) {<a name="line.503"></a>
<span class="sourceLineNo">504</span>      this.delegate = checkNotNull(delegate);<a name="line.504"></a>
<span class="sourceLineNo">505</span>    }<a name="line.505"></a>
<span class="sourceLineNo">506</span><a name="line.506"></a>
<span class="sourceLineNo">507</span>    @Override protected Multimap&lt;K, V&gt; delegate() {<a name="line.507"></a>
<span class="sourceLineNo">508</span>      return delegate;<a name="line.508"></a>
<span class="sourceLineNo">509</span>    }<a name="line.509"></a>
<span class="sourceLineNo">510</span><a name="line.510"></a>
<span class="sourceLineNo">511</span>    @Override public void clear() {<a name="line.511"></a>
<span class="sourceLineNo">512</span>      throw new UnsupportedOperationException();<a name="line.512"></a>
<span class="sourceLineNo">513</span>    }<a name="line.513"></a>
<span class="sourceLineNo">514</span><a name="line.514"></a>
<span class="sourceLineNo">515</span>    @Override public Map&lt;K, Collection&lt;V&gt;&gt; asMap() {<a name="line.515"></a>
<span class="sourceLineNo">516</span>      Map&lt;K, Collection&lt;V&gt;&gt; result = map;<a name="line.516"></a>
<span class="sourceLineNo">517</span>      if (result == null) {<a name="line.517"></a>
<span class="sourceLineNo">518</span>        final Map&lt;K, Collection&lt;V&gt;&gt; unmodifiableMap<a name="line.518"></a>
<span class="sourceLineNo">519</span>            = Collections.unmodifiableMap(delegate.asMap());<a name="line.519"></a>
<span class="sourceLineNo">520</span>        map = result = new ForwardingMap&lt;K, Collection&lt;V&gt;&gt;() {<a name="line.520"></a>
<span class="sourceLineNo">521</span>          @Override protected Map&lt;K, Collection&lt;V&gt;&gt; delegate() {<a name="line.521"></a>
<span class="sourceLineNo">522</span>            return unmodifiableMap;<a name="line.522"></a>
<span class="sourceLineNo">523</span>          }<a name="line.523"></a>
<span class="sourceLineNo">524</span><a name="line.524"></a>
<span class="sourceLineNo">525</span>          Set&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; entrySet;<a name="line.525"></a>
<span class="sourceLineNo">526</span><a name="line.526"></a>
<span class="sourceLineNo">527</span>          @Override public Set&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt; entrySet() {<a name="line.527"></a>
<span class="sourceLineNo">528</span>            Set&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; result = entrySet;<a name="line.528"></a>
<span class="sourceLineNo">529</span>            return (result == null)<a name="line.529"></a>
<span class="sourceLineNo">530</span>                ? entrySet<a name="line.530"></a>
<span class="sourceLineNo">531</span>                    = unmodifiableAsMapEntries(unmodifiableMap.entrySet())<a name="line.531"></a>
<span class="sourceLineNo">532</span>                : result;<a name="line.532"></a>
<span class="sourceLineNo">533</span>          }<a name="line.533"></a>
<span class="sourceLineNo">534</span><a name="line.534"></a>
<span class="sourceLineNo">535</span>          @Override public Collection&lt;V&gt; get(Object key) {<a name="line.535"></a>
<span class="sourceLineNo">536</span>            Collection&lt;V&gt; collection = unmodifiableMap.get(key);<a name="line.536"></a>
<span class="sourceLineNo">537</span>            return (collection == null)<a name="line.537"></a>
<span class="sourceLineNo">538</span>                ? null : unmodifiableValueCollection(collection);<a name="line.538"></a>
<span class="sourceLineNo">539</span>          }<a name="line.539"></a>
<span class="sourceLineNo">540</span><a name="line.540"></a>
<span class="sourceLineNo">541</span>          Collection&lt;Collection&lt;V&gt;&gt; asMapValues;<a name="line.541"></a>
<span class="sourceLineNo">542</span><a name="line.542"></a>
<span class="sourceLineNo">543</span>          @Override public Collection&lt;Collection&lt;V&gt;&gt; values() {<a name="line.543"></a>
<span class="sourceLineNo">544</span>            Collection&lt;Collection&lt;V&gt;&gt; result = asMapValues;<a name="line.544"></a>
<span class="sourceLineNo">545</span>            return (result == null)<a name="line.545"></a>
<span class="sourceLineNo">546</span>                ? asMapValues<a name="line.546"></a>
<span class="sourceLineNo">547</span>                    = new UnmodifiableAsMapValues&lt;V&gt;(unmodifiableMap.values())<a name="line.547"></a>
<span class="sourceLineNo">548</span>                : result;<a name="line.548"></a>
<span class="sourceLineNo">549</span>          }<a name="line.549"></a>
<span class="sourceLineNo">550</span><a name="line.550"></a>
<span class="sourceLineNo">551</span>          @Override public boolean containsValue(Object o) {<a name="line.551"></a>
<span class="sourceLineNo">552</span>            return values().contains(o);<a name="line.552"></a>
<span class="sourceLineNo">553</span>          }<a name="line.553"></a>
<span class="sourceLineNo">554</span>        };<a name="line.554"></a>
<span class="sourceLineNo">555</span>      }<a name="line.555"></a>
<span class="sourceLineNo">556</span>      return result;<a name="line.556"></a>
<span class="sourceLineNo">557</span>    }<a name="line.557"></a>
<span class="sourceLineNo">558</span><a name="line.558"></a>
<span class="sourceLineNo">559</span>    @Override public Collection&lt;Entry&lt;K, V&gt;&gt; entries() {<a name="line.559"></a>
<span class="sourceLineNo">560</span>      Collection&lt;Entry&lt;K, V&gt;&gt; result = entries;<a name="line.560"></a>
<span class="sourceLineNo">561</span>      if (result == null) {<a name="line.561"></a>
<span class="sourceLineNo">562</span>        entries = result = unmodifiableEntries(delegate.entries());<a name="line.562"></a>
<span class="sourceLineNo">563</span>      }<a name="line.563"></a>
<span class="sourceLineNo">564</span>      return result;<a name="line.564"></a>
<span class="sourceLineNo">565</span>    }<a name="line.565"></a>
<span class="sourceLineNo">566</span><a name="line.566"></a>
<span class="sourceLineNo">567</span>    @Override public Collection&lt;V&gt; get(K key) {<a name="line.567"></a>
<span class="sourceLineNo">568</span>      return unmodifiableValueCollection(delegate.get(key));<a name="line.568"></a>
<span class="sourceLineNo">569</span>    }<a name="line.569"></a>
<span class="sourceLineNo">570</span><a name="line.570"></a>
<span class="sourceLineNo">571</span>    @Override public Multiset&lt;K&gt; keys() {<a name="line.571"></a>
<span class="sourceLineNo">572</span>      Multiset&lt;K&gt; result = keys;<a name="line.572"></a>
<span class="sourceLineNo">573</span>      if (result == null) {<a name="line.573"></a>
<span class="sourceLineNo">574</span>        keys = result = Multisets.unmodifiableMultiset(delegate.keys());<a name="line.574"></a>
<span class="sourceLineNo">575</span>      }<a name="line.575"></a>
<span class="sourceLineNo">576</span>      return result;<a name="line.576"></a>
<span class="sourceLineNo">577</span>    }<a name="line.577"></a>
<span class="sourceLineNo">578</span><a name="line.578"></a>
<span class="sourceLineNo">579</span>    @Override public Set&lt;K&gt; keySet() {<a name="line.579"></a>
<span class="sourceLineNo">580</span>      Set&lt;K&gt; result = keySet;<a name="line.580"></a>
<span class="sourceLineNo">581</span>      if (result == null) {<a name="line.581"></a>
<span class="sourceLineNo">582</span>        keySet = result = Collections.unmodifiableSet(delegate.keySet());<a name="line.582"></a>
<span class="sourceLineNo">583</span>      }<a name="line.583"></a>
<span class="sourceLineNo">584</span>      return result;<a name="line.584"></a>
<span class="sourceLineNo">585</span>    }<a name="line.585"></a>
<span class="sourceLineNo">586</span><a name="line.586"></a>
<span class="sourceLineNo">587</span>    @Override public boolean put(K key, V value) {<a name="line.587"></a>
<span class="sourceLineNo">588</span>      throw new UnsupportedOperationException();<a name="line.588"></a>
<span class="sourceLineNo">589</span>    }<a name="line.589"></a>
<span class="sourceLineNo">590</span><a name="line.590"></a>
<span class="sourceLineNo">591</span>    @Override public boolean putAll(K key, Iterable&lt;? extends V&gt; values) {<a name="line.591"></a>
<span class="sourceLineNo">592</span>      throw new UnsupportedOperationException();<a name="line.592"></a>
<span class="sourceLineNo">593</span>    }<a name="line.593"></a>
<span class="sourceLineNo">594</span><a name="line.594"></a>
<span class="sourceLineNo">595</span>    @Override<a name="line.595"></a>
<span class="sourceLineNo">596</span>    public boolean putAll(Multimap&lt;? extends K, ? extends V&gt; multimap) {<a name="line.596"></a>
<span class="sourceLineNo">597</span>      throw new UnsupportedOperationException();<a name="line.597"></a>
<span class="sourceLineNo">598</span>    }<a name="line.598"></a>
<span class="sourceLineNo">599</span><a name="line.599"></a>
<span class="sourceLineNo">600</span>    @Override public boolean remove(Object key, Object value) {<a name="line.600"></a>
<span class="sourceLineNo">601</span>      throw new UnsupportedOperationException();<a name="line.601"></a>
<span class="sourceLineNo">602</span>    }<a name="line.602"></a>
<span class="sourceLineNo">603</span><a name="line.603"></a>
<span class="sourceLineNo">604</span>    @Override public Collection&lt;V&gt; removeAll(Object key) {<a name="line.604"></a>
<span class="sourceLineNo">605</span>      throw new UnsupportedOperationException();<a name="line.605"></a>
<span class="sourceLineNo">606</span>    }<a name="line.606"></a>
<span class="sourceLineNo">607</span><a name="line.607"></a>
<span class="sourceLineNo">608</span>    @Override public Collection&lt;V&gt; replaceValues(<a name="line.608"></a>
<span class="sourceLineNo">609</span>        K key, Iterable&lt;? extends V&gt; values) {<a name="line.609"></a>
<span class="sourceLineNo">610</span>      throw new UnsupportedOperationException();<a name="line.610"></a>
<span class="sourceLineNo">611</span>    }<a name="line.611"></a>
<span class="sourceLineNo">612</span><a name="line.612"></a>
<span class="sourceLineNo">613</span>    @Override public Collection&lt;V&gt; values() {<a name="line.613"></a>
<span class="sourceLineNo">614</span>      Collection&lt;V&gt; result = values;<a name="line.614"></a>
<span class="sourceLineNo">615</span>      if (result == null) {<a name="line.615"></a>
<span class="sourceLineNo">616</span>        values = result = Collections.unmodifiableCollection(delegate.values());<a name="line.616"></a>
<span class="sourceLineNo">617</span>      }<a name="line.617"></a>
<span class="sourceLineNo">618</span>      return result;<a name="line.618"></a>
<span class="sourceLineNo">619</span>    }<a name="line.619"></a>
<span class="sourceLineNo">620</span><a name="line.620"></a>
<span class="sourceLineNo">621</span>    private static final long serialVersionUID = 0;<a name="line.621"></a>
<span class="sourceLineNo">622</span>  }<a name="line.622"></a>
<span class="sourceLineNo">623</span><a name="line.623"></a>
<span class="sourceLineNo">624</span>  private static class UnmodifiableAsMapValues&lt;V&gt;<a name="line.624"></a>
<span class="sourceLineNo">625</span>      extends ForwardingCollection&lt;Collection&lt;V&gt;&gt; {<a name="line.625"></a>
<span class="sourceLineNo">626</span>    final Collection&lt;Collection&lt;V&gt;&gt; delegate;<a name="line.626"></a>
<span class="sourceLineNo">627</span>    UnmodifiableAsMapValues(Collection&lt;Collection&lt;V&gt;&gt; delegate) {<a name="line.627"></a>
<span class="sourceLineNo">628</span>      this.delegate = Collections.unmodifiableCollection(delegate);<a name="line.628"></a>
<span class="sourceLineNo">629</span>    }<a name="line.629"></a>
<span class="sourceLineNo">630</span>    @Override protected Collection&lt;Collection&lt;V&gt;&gt; delegate() {<a name="line.630"></a>
<span class="sourceLineNo">631</span>      return delegate;<a name="line.631"></a>
<span class="sourceLineNo">632</span>    }<a name="line.632"></a>
<span class="sourceLineNo">633</span>    @Override public Iterator&lt;Collection&lt;V&gt;&gt; iterator() {<a name="line.633"></a>
<span class="sourceLineNo">634</span>      final Iterator&lt;Collection&lt;V&gt;&gt; iterator = delegate.iterator();<a name="line.634"></a>
<span class="sourceLineNo">635</span>      return new UnmodifiableIterator&lt;Collection&lt;V&gt;&gt;() {<a name="line.635"></a>
<span class="sourceLineNo">636</span>        @Override<a name="line.636"></a>
<span class="sourceLineNo">637</span>        public boolean hasNext() {<a name="line.637"></a>
<span class="sourceLineNo">638</span>          return iterator.hasNext();<a name="line.638"></a>
<span class="sourceLineNo">639</span>        }<a name="line.639"></a>
<span class="sourceLineNo">640</span>        @Override<a name="line.640"></a>
<span class="sourceLineNo">641</span>        public Collection&lt;V&gt; next() {<a name="line.641"></a>
<span class="sourceLineNo">642</span>          return unmodifiableValueCollection(iterator.next());<a name="line.642"></a>
<span class="sourceLineNo">643</span>        }<a name="line.643"></a>
<span class="sourceLineNo">644</span>      };<a name="line.644"></a>
<span class="sourceLineNo">645</span>    }<a name="line.645"></a>
<span class="sourceLineNo">646</span>    @Override public Object[] toArray() {<a name="line.646"></a>
<span class="sourceLineNo">647</span>      return standardToArray();<a name="line.647"></a>
<span class="sourceLineNo">648</span>    }<a name="line.648"></a>
<span class="sourceLineNo">649</span>    @Override public &lt;T&gt; T[] toArray(T[] array) {<a name="line.649"></a>
<span class="sourceLineNo">650</span>      return standardToArray(array);<a name="line.650"></a>
<span class="sourceLineNo">651</span>    }<a name="line.651"></a>
<span class="sourceLineNo">652</span>    @Override public boolean contains(Object o) {<a name="line.652"></a>
<span class="sourceLineNo">653</span>      return standardContains(o);<a name="line.653"></a>
<span class="sourceLineNo">654</span>    }<a name="line.654"></a>
<span class="sourceLineNo">655</span>    @Override public boolean containsAll(Collection&lt;?&gt; c) {<a name="line.655"></a>
<span class="sourceLineNo">656</span>      return standardContainsAll(c);<a name="line.656"></a>
<span class="sourceLineNo">657</span>    }<a name="line.657"></a>
<span class="sourceLineNo">658</span>  }<a name="line.658"></a>
<span class="sourceLineNo">659</span><a name="line.659"></a>
<span class="sourceLineNo">660</span>  private static class UnmodifiableListMultimap&lt;K, V&gt;<a name="line.660"></a>
<span class="sourceLineNo">661</span>      extends UnmodifiableMultimap&lt;K, V&gt; implements ListMultimap&lt;K, V&gt; {<a name="line.661"></a>
<span class="sourceLineNo">662</span>    UnmodifiableListMultimap(ListMultimap&lt;K, V&gt; delegate) {<a name="line.662"></a>
<span class="sourceLineNo">663</span>      super(delegate);<a name="line.663"></a>
<span class="sourceLineNo">664</span>    }<a name="line.664"></a>
<span class="sourceLineNo">665</span>    @Override public ListMultimap&lt;K, V&gt; delegate() {<a name="line.665"></a>
<span class="sourceLineNo">666</span>      return (ListMultimap&lt;K, V&gt;) super.delegate();<a name="line.666"></a>
<span class="sourceLineNo">667</span>    }<a name="line.667"></a>
<span class="sourceLineNo">668</span>    @Override public List&lt;V&gt; get(K key) {<a name="line.668"></a>
<span class="sourceLineNo">669</span>      return Collections.unmodifiableList(delegate().get(key));<a name="line.669"></a>
<span class="sourceLineNo">670</span>    }<a name="line.670"></a>
<span class="sourceLineNo">671</span>    @Override public List&lt;V&gt; removeAll(Object key) {<a name="line.671"></a>
<span class="sourceLineNo">672</span>      throw new UnsupportedOperationException();<a name="line.672"></a>
<span class="sourceLineNo">673</span>    }<a name="line.673"></a>
<span class="sourceLineNo">674</span>    @Override public List&lt;V&gt; replaceValues(<a name="line.674"></a>
<span class="sourceLineNo">675</span>        K key, Iterable&lt;? extends V&gt; values) {<a name="line.675"></a>
<span class="sourceLineNo">676</span>      throw new UnsupportedOperationException();<a name="line.676"></a>
<span class="sourceLineNo">677</span>    }<a name="line.677"></a>
<span class="sourceLineNo">678</span>    private static final long serialVersionUID = 0;<a name="line.678"></a>
<span class="sourceLineNo">679</span>  }<a name="line.679"></a>
<span class="sourceLineNo">680</span><a name="line.680"></a>
<span class="sourceLineNo">681</span>  private static class UnmodifiableSetMultimap&lt;K, V&gt;<a name="line.681"></a>
<span class="sourceLineNo">682</span>      extends UnmodifiableMultimap&lt;K, V&gt; implements SetMultimap&lt;K, V&gt; {<a name="line.682"></a>
<span class="sourceLineNo">683</span>    UnmodifiableSetMultimap(SetMultimap&lt;K, V&gt; delegate) {<a name="line.683"></a>
<span class="sourceLineNo">684</span>      super(delegate);<a name="line.684"></a>
<span class="sourceLineNo">685</span>    }<a name="line.685"></a>
<span class="sourceLineNo">686</span>    @Override public SetMultimap&lt;K, V&gt; delegate() {<a name="line.686"></a>
<span class="sourceLineNo">687</span>      return (SetMultimap&lt;K, V&gt;) super.delegate();<a name="line.687"></a>
<span class="sourceLineNo">688</span>    }<a name="line.688"></a>
<span class="sourceLineNo">689</span>    @Override public Set&lt;V&gt; get(K key) {<a name="line.689"></a>
<span class="sourceLineNo">690</span>      /*<a name="line.690"></a>
<span class="sourceLineNo">691</span>       * Note that this doesn't return a SortedSet when delegate is a<a name="line.691"></a>
<span class="sourceLineNo">692</span>       * SortedSetMultiset, unlike (SortedSet&lt;V&gt;) super.get().<a name="line.692"></a>
<span class="sourceLineNo">693</span>       */<a name="line.693"></a>
<span class="sourceLineNo">694</span>      return Collections.unmodifiableSet(delegate().get(key));<a name="line.694"></a>
<span class="sourceLineNo">695</span>    }<a name="line.695"></a>
<span class="sourceLineNo">696</span>    @Override public Set&lt;Map.Entry&lt;K, V&gt;&gt; entries() {<a name="line.696"></a>
<span class="sourceLineNo">697</span>      return Maps.unmodifiableEntrySet(delegate().entries());<a name="line.697"></a>
<span class="sourceLineNo">698</span>    }<a name="line.698"></a>
<span class="sourceLineNo">699</span>    @Override public Set&lt;V&gt; removeAll(Object key) {<a name="line.699"></a>
<span class="sourceLineNo">700</span>      throw new UnsupportedOperationException();<a name="line.700"></a>
<span class="sourceLineNo">701</span>    }<a name="line.701"></a>
<span class="sourceLineNo">702</span>    @Override public Set&lt;V&gt; replaceValues(<a name="line.702"></a>
<span class="sourceLineNo">703</span>        K key, Iterable&lt;? extends V&gt; values) {<a name="line.703"></a>
<span class="sourceLineNo">704</span>      throw new UnsupportedOperationException();<a name="line.704"></a>
<span class="sourceLineNo">705</span>    }<a name="line.705"></a>
<span class="sourceLineNo">706</span>    private static final long serialVersionUID = 0;<a name="line.706"></a>
<span class="sourceLineNo">707</span>  }<a name="line.707"></a>
<span class="sourceLineNo">708</span><a name="line.708"></a>
<span class="sourceLineNo">709</span>  private static class UnmodifiableSortedSetMultimap&lt;K, V&gt;<a name="line.709"></a>
<span class="sourceLineNo">710</span>      extends UnmodifiableSetMultimap&lt;K, V&gt; implements SortedSetMultimap&lt;K, V&gt; {<a name="line.710"></a>
<span class="sourceLineNo">711</span>    UnmodifiableSortedSetMultimap(SortedSetMultimap&lt;K, V&gt; delegate) {<a name="line.711"></a>
<span class="sourceLineNo">712</span>      super(delegate);<a name="line.712"></a>
<span class="sourceLineNo">713</span>    }<a name="line.713"></a>
<span class="sourceLineNo">714</span>    @Override public SortedSetMultimap&lt;K, V&gt; delegate() {<a name="line.714"></a>
<span class="sourceLineNo">715</span>      return (SortedSetMultimap&lt;K, V&gt;) super.delegate();<a name="line.715"></a>
<span class="sourceLineNo">716</span>    }<a name="line.716"></a>
<span class="sourceLineNo">717</span>    @Override public SortedSet&lt;V&gt; get(K key) {<a name="line.717"></a>
<span class="sourceLineNo">718</span>      return Collections.unmodifiableSortedSet(delegate().get(key));<a name="line.718"></a>
<span class="sourceLineNo">719</span>    }<a name="line.719"></a>
<span class="sourceLineNo">720</span>    @Override public SortedSet&lt;V&gt; removeAll(Object key) {<a name="line.720"></a>
<span class="sourceLineNo">721</span>      throw new UnsupportedOperationException();<a name="line.721"></a>
<span class="sourceLineNo">722</span>    }<a name="line.722"></a>
<span class="sourceLineNo">723</span>    @Override public SortedSet&lt;V&gt; replaceValues(<a name="line.723"></a>
<span class="sourceLineNo">724</span>        K key, Iterable&lt;? extends V&gt; values) {<a name="line.724"></a>
<span class="sourceLineNo">725</span>      throw new UnsupportedOperationException();<a name="line.725"></a>
<span class="sourceLineNo">726</span>    }<a name="line.726"></a>
<span class="sourceLineNo">727</span>    @Override<a name="line.727"></a>
<span class="sourceLineNo">728</span>    public Comparator&lt;? super V&gt; valueComparator() {<a name="line.728"></a>
<span class="sourceLineNo">729</span>      return delegate().valueComparator();<a name="line.729"></a>
<span class="sourceLineNo">730</span>    }<a name="line.730"></a>
<span class="sourceLineNo">731</span>    private static final long serialVersionUID = 0;<a name="line.731"></a>
<span class="sourceLineNo">732</span>  }<a name="line.732"></a>
<span class="sourceLineNo">733</span><a name="line.733"></a>
<span class="sourceLineNo">734</span>  /**<a name="line.734"></a>
<span class="sourceLineNo">735</span>   * Returns a synchronized (thread-safe) {@code SetMultimap} backed by the<a name="line.735"></a>
<span class="sourceLineNo">736</span>   * specified multimap.<a name="line.736"></a>
<span class="sourceLineNo">737</span>   *<a name="line.737"></a>
<span class="sourceLineNo">738</span>   * &lt;p&gt;You must follow the warnings described in {@link #synchronizedMultimap}.<a name="line.738"></a>
<span class="sourceLineNo">739</span>   *<a name="line.739"></a>
<span class="sourceLineNo">740</span>   * &lt;p&gt;The returned multimap will be serializable if the specified multimap is<a name="line.740"></a>
<span class="sourceLineNo">741</span>   * serializable.<a name="line.741"></a>
<span class="sourceLineNo">742</span>   *<a name="line.742"></a>
<span class="sourceLineNo">743</span>   * @param multimap the multimap to be wrapped<a name="line.743"></a>
<span class="sourceLineNo">744</span>   * @return a synchronized view of the specified multimap<a name="line.744"></a>
<span class="sourceLineNo">745</span>   */<a name="line.745"></a>
<span class="sourceLineNo">746</span>  public static &lt;K, V&gt; SetMultimap&lt;K, V&gt; synchronizedSetMultimap(<a name="line.746"></a>
<span class="sourceLineNo">747</span>      SetMultimap&lt;K, V&gt; multimap) {<a name="line.747"></a>
<span class="sourceLineNo">748</span>    return Synchronized.setMultimap(multimap, null);<a name="line.748"></a>
<span class="sourceLineNo">749</span>  }<a name="line.749"></a>
<span class="sourceLineNo">750</span><a name="line.750"></a>
<span class="sourceLineNo">751</span>  /**<a name="line.751"></a>
<span class="sourceLineNo">752</span>   * Returns an unmodifiable view of the specified {@code SetMultimap}. Query<a name="line.752"></a>
<span class="sourceLineNo">753</span>   * operations on the returned multimap "read through" to the specified<a name="line.753"></a>
<span class="sourceLineNo">754</span>   * multimap, and attempts to modify the returned multimap, either directly or<a name="line.754"></a>
<span class="sourceLineNo">755</span>   * through the multimap's views, result in an<a name="line.755"></a>
<span class="sourceLineNo">756</span>   * {@code UnsupportedOperationException}.<a name="line.756"></a>
<span class="sourceLineNo">757</span>   *<a name="line.757"></a>
<span class="sourceLineNo">758</span>   * &lt;p&gt;Note that the generated multimap's {@link Multimap#removeAll} and<a name="line.758"></a>
<span class="sourceLineNo">759</span>   * {@link Multimap#replaceValues} methods return collections that are<a name="line.759"></a>
<span class="sourceLineNo">760</span>   * modifiable.<a name="line.760"></a>
<span class="sourceLineNo">761</span>   *<a name="line.761"></a>
<span class="sourceLineNo">762</span>   * &lt;p&gt;The returned multimap will be serializable if the specified multimap is<a name="line.762"></a>
<span class="sourceLineNo">763</span>   * serializable.<a name="line.763"></a>
<span class="sourceLineNo">764</span>   *<a name="line.764"></a>
<span class="sourceLineNo">765</span>   * @param delegate the multimap for which an unmodifiable view is to be<a name="line.765"></a>
<span class="sourceLineNo">766</span>   *     returned<a name="line.766"></a>
<span class="sourceLineNo">767</span>   * @return an unmodifiable view of the specified multimap<a name="line.767"></a>
<span class="sourceLineNo">768</span>   */<a name="line.768"></a>
<span class="sourceLineNo">769</span>  public static &lt;K, V&gt; SetMultimap&lt;K, V&gt; unmodifiableSetMultimap(<a name="line.769"></a>
<span class="sourceLineNo">770</span>      SetMultimap&lt;K, V&gt; delegate) {<a name="line.770"></a>
<span class="sourceLineNo">771</span>    if (delegate instanceof UnmodifiableSetMultimap ||<a name="line.771"></a>
<span class="sourceLineNo">772</span>        delegate instanceof ImmutableSetMultimap) {<a name="line.772"></a>
<span class="sourceLineNo">773</span>      return delegate;<a name="line.773"></a>
<span class="sourceLineNo">774</span>    }<a name="line.774"></a>
<span class="sourceLineNo">775</span>    return new UnmodifiableSetMultimap&lt;K, V&gt;(delegate);<a name="line.775"></a>
<span class="sourceLineNo">776</span>  }<a name="line.776"></a>
<span class="sourceLineNo">777</span><a name="line.777"></a>
<span class="sourceLineNo">778</span>  /**<a name="line.778"></a>
<span class="sourceLineNo">779</span>   * Simply returns its argument.<a name="line.779"></a>
<span class="sourceLineNo">780</span>   *<a name="line.780"></a>
<span class="sourceLineNo">781</span>   * @deprecated no need to use this<a name="line.781"></a>
<span class="sourceLineNo">782</span>   * @since 10.0<a name="line.782"></a>
<span class="sourceLineNo">783</span>   */<a name="line.783"></a>
<span class="sourceLineNo">784</span>  @Deprecated public static &lt;K, V&gt; SetMultimap&lt;K, V&gt; unmodifiableSetMultimap(<a name="line.784"></a>
<span class="sourceLineNo">785</span>      ImmutableSetMultimap&lt;K, V&gt; delegate) {<a name="line.785"></a>
<span class="sourceLineNo">786</span>    return checkNotNull(delegate);<a name="line.786"></a>
<span class="sourceLineNo">787</span>  }<a name="line.787"></a>
<span class="sourceLineNo">788</span><a name="line.788"></a>
<span class="sourceLineNo">789</span>  /**<a name="line.789"></a>
<span class="sourceLineNo">790</span>   * Returns a synchronized (thread-safe) {@code SortedSetMultimap} backed by<a name="line.790"></a>
<span class="sourceLineNo">791</span>   * the specified multimap.<a name="line.791"></a>
<span class="sourceLineNo">792</span>   *<a name="line.792"></a>
<span class="sourceLineNo">793</span>   * &lt;p&gt;You must follow the warnings described in {@link #synchronizedMultimap}.<a name="line.793"></a>
<span class="sourceLineNo">794</span>   *<a name="line.794"></a>
<span class="sourceLineNo">795</span>   * &lt;p&gt;The returned multimap will be serializable if the specified multimap is<a name="line.795"></a>
<span class="sourceLineNo">796</span>   * serializable.<a name="line.796"></a>
<span class="sourceLineNo">797</span>   *<a name="line.797"></a>
<span class="sourceLineNo">798</span>   * @param multimap the multimap to be wrapped<a name="line.798"></a>
<span class="sourceLineNo">799</span>   * @return a synchronized view of the specified multimap<a name="line.799"></a>
<span class="sourceLineNo">800</span>   */<a name="line.800"></a>
<span class="sourceLineNo">801</span>  public static &lt;K, V&gt; SortedSetMultimap&lt;K, V&gt;<a name="line.801"></a>
<span class="sourceLineNo">802</span>      synchronizedSortedSetMultimap(SortedSetMultimap&lt;K, V&gt; multimap) {<a name="line.802"></a>
<span class="sourceLineNo">803</span>    return Synchronized.sortedSetMultimap(multimap, null);<a name="line.803"></a>
<span class="sourceLineNo">804</span>  }<a name="line.804"></a>
<span class="sourceLineNo">805</span><a name="line.805"></a>
<span class="sourceLineNo">806</span>  /**<a name="line.806"></a>
<span class="sourceLineNo">807</span>   * Returns an unmodifiable view of the specified {@code SortedSetMultimap}.<a name="line.807"></a>
<span class="sourceLineNo">808</span>   * Query operations on the returned multimap "read through" to the specified<a name="line.808"></a>
<span class="sourceLineNo">809</span>   * multimap, and attempts to modify the returned multimap, either directly or<a name="line.809"></a>
<span class="sourceLineNo">810</span>   * through the multimap's views, result in an<a name="line.810"></a>
<span class="sourceLineNo">811</span>   * {@code UnsupportedOperationException}.<a name="line.811"></a>
<span class="sourceLineNo">812</span>   *<a name="line.812"></a>
<span class="sourceLineNo">813</span>   * &lt;p&gt;Note that the generated multimap's {@link Multimap#removeAll} and<a name="line.813"></a>
<span class="sourceLineNo">814</span>   * {@link Multimap#replaceValues} methods return collections that are<a name="line.814"></a>
<span class="sourceLineNo">815</span>   * modifiable.<a name="line.815"></a>
<span class="sourceLineNo">816</span>   *<a name="line.816"></a>
<span class="sourceLineNo">817</span>   * &lt;p&gt;The returned multimap will be serializable if the specified multimap is<a name="line.817"></a>
<span class="sourceLineNo">818</span>   * serializable.<a name="line.818"></a>
<span class="sourceLineNo">819</span>   *<a name="line.819"></a>
<span class="sourceLineNo">820</span>   * @param delegate the multimap for which an unmodifiable view is to be<a name="line.820"></a>
<span class="sourceLineNo">821</span>   *     returned<a name="line.821"></a>
<span class="sourceLineNo">822</span>   * @return an unmodifiable view of the specified multimap<a name="line.822"></a>
<span class="sourceLineNo">823</span>   */<a name="line.823"></a>
<span class="sourceLineNo">824</span>  public static &lt;K, V&gt; SortedSetMultimap&lt;K, V&gt; unmodifiableSortedSetMultimap(<a name="line.824"></a>
<span class="sourceLineNo">825</span>      SortedSetMultimap&lt;K, V&gt; delegate) {<a name="line.825"></a>
<span class="sourceLineNo">826</span>    if (delegate instanceof UnmodifiableSortedSetMultimap) {<a name="line.826"></a>
<span class="sourceLineNo">827</span>      return delegate;<a name="line.827"></a>
<span class="sourceLineNo">828</span>    }<a name="line.828"></a>
<span class="sourceLineNo">829</span>    return new UnmodifiableSortedSetMultimap&lt;K, V&gt;(delegate);<a name="line.829"></a>
<span class="sourceLineNo">830</span>  }<a name="line.830"></a>
<span class="sourceLineNo">831</span><a name="line.831"></a>
<span class="sourceLineNo">832</span>  /**<a name="line.832"></a>
<span class="sourceLineNo">833</span>   * Returns a synchronized (thread-safe) {@code ListMultimap} backed by the<a name="line.833"></a>
<span class="sourceLineNo">834</span>   * specified multimap.<a name="line.834"></a>
<span class="sourceLineNo">835</span>   *<a name="line.835"></a>
<span class="sourceLineNo">836</span>   * &lt;p&gt;You must follow the warnings described in {@link #synchronizedMultimap}.<a name="line.836"></a>
<span class="sourceLineNo">837</span>   *<a name="line.837"></a>
<span class="sourceLineNo">838</span>   * @param multimap the multimap to be wrapped<a name="line.838"></a>
<span class="sourceLineNo">839</span>   * @return a synchronized view of the specified multimap<a name="line.839"></a>
<span class="sourceLineNo">840</span>   */<a name="line.840"></a>
<span class="sourceLineNo">841</span>  public static &lt;K, V&gt; ListMultimap&lt;K, V&gt; synchronizedListMultimap(<a name="line.841"></a>
<span class="sourceLineNo">842</span>      ListMultimap&lt;K, V&gt; multimap) {<a name="line.842"></a>
<span class="sourceLineNo">843</span>    return Synchronized.listMultimap(multimap, null);<a name="line.843"></a>
<span class="sourceLineNo">844</span>  }<a name="line.844"></a>
<span class="sourceLineNo">845</span><a name="line.845"></a>
<span class="sourceLineNo">846</span>  /**<a name="line.846"></a>
<span class="sourceLineNo">847</span>   * Returns an unmodifiable view of the specified {@code ListMultimap}. Query<a name="line.847"></a>
<span class="sourceLineNo">848</span>   * operations on the returned multimap "read through" to the specified<a name="line.848"></a>
<span class="sourceLineNo">849</span>   * multimap, and attempts to modify the returned multimap, either directly or<a name="line.849"></a>
<span class="sourceLineNo">850</span>   * through the multimap's views, result in an<a name="line.850"></a>
<span class="sourceLineNo">851</span>   * {@code UnsupportedOperationException}.<a name="line.851"></a>
<span class="sourceLineNo">852</span>   *<a name="line.852"></a>
<span class="sourceLineNo">853</span>   * &lt;p&gt;Note that the generated multimap's {@link Multimap#removeAll} and<a name="line.853"></a>
<span class="sourceLineNo">854</span>   * {@link Multimap#replaceValues} methods return collections that are<a name="line.854"></a>
<span class="sourceLineNo">855</span>   * modifiable.<a name="line.855"></a>
<span class="sourceLineNo">856</span>   *<a name="line.856"></a>
<span class="sourceLineNo">857</span>   * &lt;p&gt;The returned multimap will be serializable if the specified multimap is<a name="line.857"></a>
<span class="sourceLineNo">858</span>   * serializable.<a name="line.858"></a>
<span class="sourceLineNo">859</span>   *<a name="line.859"></a>
<span class="sourceLineNo">860</span>   * @param delegate the multimap for which an unmodifiable view is to be<a name="line.860"></a>
<span class="sourceLineNo">861</span>   *     returned<a name="line.861"></a>
<span class="sourceLineNo">862</span>   * @return an unmodifiable view of the specified multimap<a name="line.862"></a>
<span class="sourceLineNo">863</span>   */<a name="line.863"></a>
<span class="sourceLineNo">864</span>  public static &lt;K, V&gt; ListMultimap&lt;K, V&gt; unmodifiableListMultimap(<a name="line.864"></a>
<span class="sourceLineNo">865</span>      ListMultimap&lt;K, V&gt; delegate) {<a name="line.865"></a>
<span class="sourceLineNo">866</span>    if (delegate instanceof UnmodifiableListMultimap ||<a name="line.866"></a>
<span class="sourceLineNo">867</span>        delegate instanceof ImmutableListMultimap) {<a name="line.867"></a>
<span class="sourceLineNo">868</span>      return delegate;<a name="line.868"></a>
<span class="sourceLineNo">869</span>    }<a name="line.869"></a>
<span class="sourceLineNo">870</span>    return new UnmodifiableListMultimap&lt;K, V&gt;(delegate);<a name="line.870"></a>
<span class="sourceLineNo">871</span>  }<a name="line.871"></a>
<span class="sourceLineNo">872</span><a name="line.872"></a>
<span class="sourceLineNo">873</span>  /**<a name="line.873"></a>
<span class="sourceLineNo">874</span>   * Simply returns its argument.<a name="line.874"></a>
<span class="sourceLineNo">875</span>   *<a name="line.875"></a>
<span class="sourceLineNo">876</span>   * @deprecated no need to use this<a name="line.876"></a>
<span class="sourceLineNo">877</span>   * @since 10.0<a name="line.877"></a>
<span class="sourceLineNo">878</span>   */<a name="line.878"></a>
<span class="sourceLineNo">879</span>  @Deprecated public static &lt;K, V&gt; ListMultimap&lt;K, V&gt; unmodifiableListMultimap(<a name="line.879"></a>
<span class="sourceLineNo">880</span>      ImmutableListMultimap&lt;K, V&gt; delegate) {<a name="line.880"></a>
<span class="sourceLineNo">881</span>    return checkNotNull(delegate);<a name="line.881"></a>
<span class="sourceLineNo">882</span>  }<a name="line.882"></a>
<span class="sourceLineNo">883</span><a name="line.883"></a>
<span class="sourceLineNo">884</span>  /**<a name="line.884"></a>
<span class="sourceLineNo">885</span>   * Returns an unmodifiable view of the specified collection, preserving the<a name="line.885"></a>
<span class="sourceLineNo">886</span>   * interface for instances of {@code SortedSet}, {@code Set}, {@code List} and<a name="line.886"></a>
<span class="sourceLineNo">887</span>   * {@code Collection}, in that order of preference.<a name="line.887"></a>
<span class="sourceLineNo">888</span>   *<a name="line.888"></a>
<span class="sourceLineNo">889</span>   * @param collection the collection for which to return an unmodifiable view<a name="line.889"></a>
<span class="sourceLineNo">890</span>   * @return an unmodifiable view of the collection<a name="line.890"></a>
<span class="sourceLineNo">891</span>   */<a name="line.891"></a>
<span class="sourceLineNo">892</span>  private static &lt;V&gt; Collection&lt;V&gt; unmodifiableValueCollection(<a name="line.892"></a>
<span class="sourceLineNo">893</span>      Collection&lt;V&gt; collection) {<a name="line.893"></a>
<span class="sourceLineNo">894</span>    if (collection instanceof SortedSet) {<a name="line.894"></a>
<span class="sourceLineNo">895</span>      return Collections.unmodifiableSortedSet((SortedSet&lt;V&gt;) collection);<a name="line.895"></a>
<span class="sourceLineNo">896</span>    } else if (collection instanceof Set) {<a name="line.896"></a>
<span class="sourceLineNo">897</span>      return Collections.unmodifiableSet((Set&lt;V&gt;) collection);<a name="line.897"></a>
<span class="sourceLineNo">898</span>    } else if (collection instanceof List) {<a name="line.898"></a>
<span class="sourceLineNo">899</span>      return Collections.unmodifiableList((List&lt;V&gt;) collection);<a name="line.899"></a>
<span class="sourceLineNo">900</span>    }<a name="line.900"></a>
<span class="sourceLineNo">901</span>    return Collections.unmodifiableCollection(collection);<a name="line.901"></a>
<span class="sourceLineNo">902</span>  }<a name="line.902"></a>
<span class="sourceLineNo">903</span><a name="line.903"></a>
<span class="sourceLineNo">904</span>  /**<a name="line.904"></a>
<span class="sourceLineNo">905</span>   * Returns an unmodifiable view of the specified multimap {@code asMap} entry.<a name="line.905"></a>
<span class="sourceLineNo">906</span>   * The {@link Entry#setValue} operation throws an {@link<a name="line.906"></a>
<span class="sourceLineNo">907</span>   * UnsupportedOperationException}, and the collection returned by {@code<a name="line.907"></a>
<span class="sourceLineNo">908</span>   * getValue} is also an unmodifiable (type-preserving) view. This also has the<a name="line.908"></a>
<span class="sourceLineNo">909</span>   * side-effect of redefining equals to comply with the Map.Entry contract, and<a name="line.909"></a>
<span class="sourceLineNo">910</span>   * to avoid a possible nefarious implementation of equals.<a name="line.910"></a>
<span class="sourceLineNo">911</span>   *<a name="line.911"></a>
<span class="sourceLineNo">912</span>   * @param entry the entry for which to return an unmodifiable view<a name="line.912"></a>
<span class="sourceLineNo">913</span>   * @return an unmodifiable view of the entry<a name="line.913"></a>
<span class="sourceLineNo">914</span>   */<a name="line.914"></a>
<span class="sourceLineNo">915</span>  private static &lt;K, V&gt; Map.Entry&lt;K, Collection&lt;V&gt;&gt; unmodifiableAsMapEntry(<a name="line.915"></a>
<span class="sourceLineNo">916</span>      final Map.Entry&lt;K, Collection&lt;V&gt;&gt; entry) {<a name="line.916"></a>
<span class="sourceLineNo">917</span>    checkNotNull(entry);<a name="line.917"></a>
<span class="sourceLineNo">918</span>    return new AbstractMapEntry&lt;K, Collection&lt;V&gt;&gt;() {<a name="line.918"></a>
<span class="sourceLineNo">919</span>      @Override public K getKey() {<a name="line.919"></a>
<span class="sourceLineNo">920</span>        return entry.getKey();<a name="line.920"></a>
<span class="sourceLineNo">921</span>      }<a name="line.921"></a>
<span class="sourceLineNo">922</span><a name="line.922"></a>
<span class="sourceLineNo">923</span>      @Override public Collection&lt;V&gt; getValue() {<a name="line.923"></a>
<span class="sourceLineNo">924</span>        return unmodifiableValueCollection(entry.getValue());<a name="line.924"></a>
<span class="sourceLineNo">925</span>      }<a name="line.925"></a>
<span class="sourceLineNo">926</span>    };<a name="line.926"></a>
<span class="sourceLineNo">927</span>  }<a name="line.927"></a>
<span class="sourceLineNo">928</span><a name="line.928"></a>
<span class="sourceLineNo">929</span>  /**<a name="line.929"></a>
<span class="sourceLineNo">930</span>   * Returns an unmodifiable view of the specified collection of entries. The<a name="line.930"></a>
<span class="sourceLineNo">931</span>   * {@link Entry#setValue} operation throws an {@link<a name="line.931"></a>
<span class="sourceLineNo">932</span>   * UnsupportedOperationException}. If the specified collection is a {@code<a name="line.932"></a>
<span class="sourceLineNo">933</span>   * Set}, the returned collection is also a {@code Set}.<a name="line.933"></a>
<span class="sourceLineNo">934</span>   *<a name="line.934"></a>
<span class="sourceLineNo">935</span>   * @param entries the entries for which to return an unmodifiable view<a name="line.935"></a>
<span class="sourceLineNo">936</span>   * @return an unmodifiable view of the entries<a name="line.936"></a>
<span class="sourceLineNo">937</span>   */<a name="line.937"></a>
<span class="sourceLineNo">938</span>  private static &lt;K, V&gt; Collection&lt;Entry&lt;K, V&gt;&gt; unmodifiableEntries(<a name="line.938"></a>
<span class="sourceLineNo">939</span>      Collection&lt;Entry&lt;K, V&gt;&gt; entries) {<a name="line.939"></a>
<span class="sourceLineNo">940</span>    if (entries instanceof Set) {<a name="line.940"></a>
<span class="sourceLineNo">941</span>      return Maps.unmodifiableEntrySet((Set&lt;Entry&lt;K, V&gt;&gt;) entries);<a name="line.941"></a>
<span class="sourceLineNo">942</span>    }<a name="line.942"></a>
<span class="sourceLineNo">943</span>    return new Maps.UnmodifiableEntries&lt;K, V&gt;(<a name="line.943"></a>
<span class="sourceLineNo">944</span>        Collections.unmodifiableCollection(entries));<a name="line.944"></a>
<span class="sourceLineNo">945</span>  }<a name="line.945"></a>
<span class="sourceLineNo">946</span><a name="line.946"></a>
<span class="sourceLineNo">947</span>  /**<a name="line.947"></a>
<span class="sourceLineNo">948</span>   * Returns an unmodifiable view of the specified set of {@code asMap} entries.<a name="line.948"></a>
<span class="sourceLineNo">949</span>   * The {@link Entry#setValue} operation throws an {@link<a name="line.949"></a>
<span class="sourceLineNo">950</span>   * UnsupportedOperationException}, as do any operations that attempt to modify<a name="line.950"></a>
<span class="sourceLineNo">951</span>   * the returned collection.<a name="line.951"></a>
<span class="sourceLineNo">952</span>   *<a name="line.952"></a>
<span class="sourceLineNo">953</span>   * @param asMapEntries the {@code asMap} entries for which to return an<a name="line.953"></a>
<span class="sourceLineNo">954</span>   *     unmodifiable view<a name="line.954"></a>
<span class="sourceLineNo">955</span>   * @return an unmodifiable view of the collection entries<a name="line.955"></a>
<span class="sourceLineNo">956</span>   */<a name="line.956"></a>
<span class="sourceLineNo">957</span>  private static &lt;K, V&gt; Set&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; unmodifiableAsMapEntries(<a name="line.957"></a>
<span class="sourceLineNo">958</span>      Set&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; asMapEntries) {<a name="line.958"></a>
<span class="sourceLineNo">959</span>    return new UnmodifiableAsMapEntries&lt;K, V&gt;(<a name="line.959"></a>
<span class="sourceLineNo">960</span>        Collections.unmodifiableSet(asMapEntries));<a name="line.960"></a>
<span class="sourceLineNo">961</span>  }<a name="line.961"></a>
<span class="sourceLineNo">962</span><a name="line.962"></a>
<span class="sourceLineNo">963</span>  /** @see Multimaps#unmodifiableAsMapEntries */<a name="line.963"></a>
<span class="sourceLineNo">964</span>  static class UnmodifiableAsMapEntries&lt;K, V&gt;<a name="line.964"></a>
<span class="sourceLineNo">965</span>      extends ForwardingSet&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; {<a name="line.965"></a>
<span class="sourceLineNo">966</span>    private final Set&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; delegate;<a name="line.966"></a>
<span class="sourceLineNo">967</span>    UnmodifiableAsMapEntries(Set&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; delegate) {<a name="line.967"></a>
<span class="sourceLineNo">968</span>      this.delegate = delegate;<a name="line.968"></a>
<span class="sourceLineNo">969</span>    }<a name="line.969"></a>
<span class="sourceLineNo">970</span><a name="line.970"></a>
<span class="sourceLineNo">971</span>    @Override protected Set&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; delegate() {<a name="line.971"></a>
<span class="sourceLineNo">972</span>      return delegate;<a name="line.972"></a>
<span class="sourceLineNo">973</span>    }<a name="line.973"></a>
<span class="sourceLineNo">974</span><a name="line.974"></a>
<span class="sourceLineNo">975</span>    @Override public Iterator&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; iterator() {<a name="line.975"></a>
<span class="sourceLineNo">976</span>      final Iterator&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; iterator = delegate.iterator();<a name="line.976"></a>
<span class="sourceLineNo">977</span>      return new ForwardingIterator&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt;() {<a name="line.977"></a>
<span class="sourceLineNo">978</span>        @Override protected Iterator&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; delegate() {<a name="line.978"></a>
<span class="sourceLineNo">979</span>          return iterator;<a name="line.979"></a>
<span class="sourceLineNo">980</span>        }<a name="line.980"></a>
<span class="sourceLineNo">981</span>        @Override public Entry&lt;K, Collection&lt;V&gt;&gt; next() {<a name="line.981"></a>
<span class="sourceLineNo">982</span>          return unmodifiableAsMapEntry(iterator.next());<a name="line.982"></a>
<span class="sourceLineNo">983</span>        }<a name="line.983"></a>
<span class="sourceLineNo">984</span>      };<a name="line.984"></a>
<span class="sourceLineNo">985</span>    }<a name="line.985"></a>
<span class="sourceLineNo">986</span><a name="line.986"></a>
<span class="sourceLineNo">987</span>    @Override public Object[] toArray() {<a name="line.987"></a>
<span class="sourceLineNo">988</span>      return standardToArray();<a name="line.988"></a>
<span class="sourceLineNo">989</span>    }<a name="line.989"></a>
<span class="sourceLineNo">990</span><a name="line.990"></a>
<span class="sourceLineNo">991</span>    @Override public &lt;T&gt; T[] toArray(T[] array) {<a name="line.991"></a>
<span class="sourceLineNo">992</span>      return standardToArray(array);<a name="line.992"></a>
<span class="sourceLineNo">993</span>    }<a name="line.993"></a>
<span class="sourceLineNo">994</span><a name="line.994"></a>
<span class="sourceLineNo">995</span>    @Override public boolean contains(Object o) {<a name="line.995"></a>
<span class="sourceLineNo">996</span>      return Maps.containsEntryImpl(delegate(), o);<a name="line.996"></a>
<span class="sourceLineNo">997</span>    }<a name="line.997"></a>
<span class="sourceLineNo">998</span><a name="line.998"></a>
<span class="sourceLineNo">999</span>    @Override public boolean containsAll(Collection&lt;?&gt; c) {<a name="line.999"></a>
<span class="sourceLineNo">1000</span>      return standardContainsAll(c);<a name="line.1000"></a>
<span class="sourceLineNo">1001</span>    }<a name="line.1001"></a>
<span class="sourceLineNo">1002</span><a name="line.1002"></a>
<span class="sourceLineNo">1003</span>    @Override public boolean equals(@Nullable Object object) {<a name="line.1003"></a>
<span class="sourceLineNo">1004</span>      return standardEquals(object);<a name="line.1004"></a>
<span class="sourceLineNo">1005</span>    }<a name="line.1005"></a>
<span class="sourceLineNo">1006</span>  }<a name="line.1006"></a>
<span class="sourceLineNo">1007</span><a name="line.1007"></a>
<span class="sourceLineNo">1008</span>  /**<a name="line.1008"></a>
<span class="sourceLineNo">1009</span>   * Returns a multimap view of the specified map. The multimap is backed by the<a name="line.1009"></a>
<span class="sourceLineNo">1010</span>   * map, so changes to the map are reflected in the multimap, and vice versa.<a name="line.1010"></a>
<span class="sourceLineNo">1011</span>   * If the map is modified while an iteration over one of the multimap's<a name="line.1011"></a>
<span class="sourceLineNo">1012</span>   * collection views is in progress (except through the iterator's own {@code<a name="line.1012"></a>
<span class="sourceLineNo">1013</span>   * remove} operation, or through the {@code setValue} operation on a map entry<a name="line.1013"></a>
<span class="sourceLineNo">1014</span>   * returned by the iterator), the results of the iteration are undefined.<a name="line.1014"></a>
<span class="sourceLineNo">1015</span>   *<a name="line.1015"></a>
<span class="sourceLineNo">1016</span>   * &lt;p&gt;The multimap supports mapping removal, which removes the corresponding<a name="line.1016"></a>
<span class="sourceLineNo">1017</span>   * mapping from the map. It does not support any operations which might add<a name="line.1017"></a>
<span class="sourceLineNo">1018</span>   * mappings, such as {@code put}, {@code putAll} or {@code replaceValues}.<a name="line.1018"></a>
<span class="sourceLineNo">1019</span>   *<a name="line.1019"></a>
<span class="sourceLineNo">1020</span>   * &lt;p&gt;The returned multimap will be serializable if the specified map is<a name="line.1020"></a>
<span class="sourceLineNo">1021</span>   * serializable.<a name="line.1021"></a>
<span class="sourceLineNo">1022</span>   *<a name="line.1022"></a>
<span class="sourceLineNo">1023</span>   * @param map the backing map for the returned multimap view<a name="line.1023"></a>
<span class="sourceLineNo">1024</span>   */<a name="line.1024"></a>
<span class="sourceLineNo">1025</span>  public static &lt;K, V&gt; SetMultimap&lt;K, V&gt; forMap(Map&lt;K, V&gt; map) {<a name="line.1025"></a>
<span class="sourceLineNo">1026</span>    return new MapMultimap&lt;K, V&gt;(map);<a name="line.1026"></a>
<span class="sourceLineNo">1027</span>  }<a name="line.1027"></a>
<span class="sourceLineNo">1028</span><a name="line.1028"></a>
<span class="sourceLineNo">1029</span>  /** @see Multimaps#forMap */<a name="line.1029"></a>
<span class="sourceLineNo">1030</span>  private static class MapMultimap&lt;K, V&gt;<a name="line.1030"></a>
<span class="sourceLineNo">1031</span>      implements SetMultimap&lt;K, V&gt;, Serializable {<a name="line.1031"></a>
<span class="sourceLineNo">1032</span>    final Map&lt;K, V&gt; map;<a name="line.1032"></a>
<span class="sourceLineNo">1033</span>    transient Map&lt;K, Collection&lt;V&gt;&gt; asMap;<a name="line.1033"></a>
<span class="sourceLineNo">1034</span><a name="line.1034"></a>
<span class="sourceLineNo">1035</span>    MapMultimap(Map&lt;K, V&gt; map) {<a name="line.1035"></a>
<span class="sourceLineNo">1036</span>      this.map = checkNotNull(map);<a name="line.1036"></a>
<span class="sourceLineNo">1037</span>    }<a name="line.1037"></a>
<span class="sourceLineNo">1038</span><a name="line.1038"></a>
<span class="sourceLineNo">1039</span>    @Override<a name="line.1039"></a>
<span class="sourceLineNo">1040</span>    public int size() {<a name="line.1040"></a>
<span class="sourceLineNo">1041</span>      return map.size();<a name="line.1041"></a>
<span class="sourceLineNo">1042</span>    }<a name="line.1042"></a>
<span class="sourceLineNo">1043</span><a name="line.1043"></a>
<span class="sourceLineNo">1044</span>    @Override<a name="line.1044"></a>
<span class="sourceLineNo">1045</span>    public boolean isEmpty() {<a name="line.1045"></a>
<span class="sourceLineNo">1046</span>      return map.isEmpty();<a name="line.1046"></a>
<span class="sourceLineNo">1047</span>    }<a name="line.1047"></a>
<span class="sourceLineNo">1048</span><a name="line.1048"></a>
<span class="sourceLineNo">1049</span>    @Override<a name="line.1049"></a>
<span class="sourceLineNo">1050</span>    public boolean containsKey(Object key) {<a name="line.1050"></a>
<span class="sourceLineNo">1051</span>      return map.containsKey(key);<a name="line.1051"></a>
<span class="sourceLineNo">1052</span>    }<a name="line.1052"></a>
<span class="sourceLineNo">1053</span><a name="line.1053"></a>
<span class="sourceLineNo">1054</span>    @Override<a name="line.1054"></a>
<span class="sourceLineNo">1055</span>    public boolean containsValue(Object value) {<a name="line.1055"></a>
<span class="sourceLineNo">1056</span>      return map.containsValue(value);<a name="line.1056"></a>
<span class="sourceLineNo">1057</span>    }<a name="line.1057"></a>
<span class="sourceLineNo">1058</span><a name="line.1058"></a>
<span class="sourceLineNo">1059</span>    @Override<a name="line.1059"></a>
<span class="sourceLineNo">1060</span>    public boolean containsEntry(Object key, Object value) {<a name="line.1060"></a>
<span class="sourceLineNo">1061</span>      return map.entrySet().contains(Maps.immutableEntry(key, value));<a name="line.1061"></a>
<span class="sourceLineNo">1062</span>    }<a name="line.1062"></a>
<span class="sourceLineNo">1063</span><a name="line.1063"></a>
<span class="sourceLineNo">1064</span>    @Override<a name="line.1064"></a>
<span class="sourceLineNo">1065</span>    public Set&lt;V&gt; get(final K key) {<a name="line.1065"></a>
<span class="sourceLineNo">1066</span>      return new Sets.ImprovedAbstractSet&lt;V&gt;() {<a name="line.1066"></a>
<span class="sourceLineNo">1067</span>        @Override public Iterator&lt;V&gt; iterator() {<a name="line.1067"></a>
<span class="sourceLineNo">1068</span>          return new Iterator&lt;V&gt;() {<a name="line.1068"></a>
<span class="sourceLineNo">1069</span>            int i;<a name="line.1069"></a>
<span class="sourceLineNo">1070</span><a name="line.1070"></a>
<span class="sourceLineNo">1071</span>            @Override<a name="line.1071"></a>
<span class="sourceLineNo">1072</span>            public boolean hasNext() {<a name="line.1072"></a>
<span class="sourceLineNo">1073</span>              return (i == 0) &amp;&amp; map.containsKey(key);<a name="line.1073"></a>
<span class="sourceLineNo">1074</span>            }<a name="line.1074"></a>
<span class="sourceLineNo">1075</span><a name="line.1075"></a>
<span class="sourceLineNo">1076</span>            @Override<a name="line.1076"></a>
<span class="sourceLineNo">1077</span>            public V next() {<a name="line.1077"></a>
<span class="sourceLineNo">1078</span>              if (!hasNext()) {<a name="line.1078"></a>
<span class="sourceLineNo">1079</span>                throw new NoSuchElementException();<a name="line.1079"></a>
<span class="sourceLineNo">1080</span>              }<a name="line.1080"></a>
<span class="sourceLineNo">1081</span>              i++;<a name="line.1081"></a>
<span class="sourceLineNo">1082</span>              return map.get(key);<a name="line.1082"></a>
<span class="sourceLineNo">1083</span>            }<a name="line.1083"></a>
<span class="sourceLineNo">1084</span><a name="line.1084"></a>
<span class="sourceLineNo">1085</span>            @Override<a name="line.1085"></a>
<span class="sourceLineNo">1086</span>            public void remove() {<a name="line.1086"></a>
<span class="sourceLineNo">1087</span>              checkState(i == 1);<a name="line.1087"></a>
<span class="sourceLineNo">1088</span>              i = -1;<a name="line.1088"></a>
<span class="sourceLineNo">1089</span>              map.remove(key);<a name="line.1089"></a>
<span class="sourceLineNo">1090</span>            }<a name="line.1090"></a>
<span class="sourceLineNo">1091</span>          };<a name="line.1091"></a>
<span class="sourceLineNo">1092</span>        }<a name="line.1092"></a>
<span class="sourceLineNo">1093</span><a name="line.1093"></a>
<span class="sourceLineNo">1094</span>        @Override public int size() {<a name="line.1094"></a>
<span class="sourceLineNo">1095</span>          return map.containsKey(key) ? 1 : 0;<a name="line.1095"></a>
<span class="sourceLineNo">1096</span>        }<a name="line.1096"></a>
<span class="sourceLineNo">1097</span>      };<a name="line.1097"></a>
<span class="sourceLineNo">1098</span>    }<a name="line.1098"></a>
<span class="sourceLineNo">1099</span><a name="line.1099"></a>
<span class="sourceLineNo">1100</span>    @Override<a name="line.1100"></a>
<span class="sourceLineNo">1101</span>    public boolean put(K key, V value) {<a name="line.1101"></a>
<span class="sourceLineNo">1102</span>      throw new UnsupportedOperationException();<a name="line.1102"></a>
<span class="sourceLineNo">1103</span>    }<a name="line.1103"></a>
<span class="sourceLineNo">1104</span><a name="line.1104"></a>
<span class="sourceLineNo">1105</span>    @Override<a name="line.1105"></a>
<span class="sourceLineNo">1106</span>    public boolean putAll(K key, Iterable&lt;? extends V&gt; values) {<a name="line.1106"></a>
<span class="sourceLineNo">1107</span>      throw new UnsupportedOperationException();<a name="line.1107"></a>
<span class="sourceLineNo">1108</span>    }<a name="line.1108"></a>
<span class="sourceLineNo">1109</span><a name="line.1109"></a>
<span class="sourceLineNo">1110</span>    @Override<a name="line.1110"></a>
<span class="sourceLineNo">1111</span>    public boolean putAll(Multimap&lt;? extends K, ? extends V&gt; multimap) {<a name="line.1111"></a>
<span class="sourceLineNo">1112</span>      throw new UnsupportedOperationException();<a name="line.1112"></a>
<span class="sourceLineNo">1113</span>    }<a name="line.1113"></a>
<span class="sourceLineNo">1114</span><a name="line.1114"></a>
<span class="sourceLineNo">1115</span>    @Override<a name="line.1115"></a>
<span class="sourceLineNo">1116</span>    public Set&lt;V&gt; replaceValues(K key, Iterable&lt;? extends V&gt; values) {<a name="line.1116"></a>
<span class="sourceLineNo">1117</span>      throw new UnsupportedOperationException();<a name="line.1117"></a>
<span class="sourceLineNo">1118</span>    }<a name="line.1118"></a>
<span class="sourceLineNo">1119</span><a name="line.1119"></a>
<span class="sourceLineNo">1120</span>    @Override<a name="line.1120"></a>
<span class="sourceLineNo">1121</span>    public boolean remove(Object key, Object value) {<a name="line.1121"></a>
<span class="sourceLineNo">1122</span>      return map.entrySet().remove(Maps.immutableEntry(key, value));<a name="line.1122"></a>
<span class="sourceLineNo">1123</span>    }<a name="line.1123"></a>
<span class="sourceLineNo">1124</span><a name="line.1124"></a>
<span class="sourceLineNo">1125</span>    @Override<a name="line.1125"></a>
<span class="sourceLineNo">1126</span>    public Set&lt;V&gt; removeAll(Object key) {<a name="line.1126"></a>
<span class="sourceLineNo">1127</span>      Set&lt;V&gt; values = new HashSet&lt;V&gt;(2);<a name="line.1127"></a>
<span class="sourceLineNo">1128</span>      if (!map.containsKey(key)) {<a name="line.1128"></a>
<span class="sourceLineNo">1129</span>        return values;<a name="line.1129"></a>
<span class="sourceLineNo">1130</span>      }<a name="line.1130"></a>
<span class="sourceLineNo">1131</span>      values.add(map.remove(key));<a name="line.1131"></a>
<span class="sourceLineNo">1132</span>      return values;<a name="line.1132"></a>
<span class="sourceLineNo">1133</span>    }<a name="line.1133"></a>
<span class="sourceLineNo">1134</span><a name="line.1134"></a>
<span class="sourceLineNo">1135</span>    @Override<a name="line.1135"></a>
<span class="sourceLineNo">1136</span>    public void clear() {<a name="line.1136"></a>
<span class="sourceLineNo">1137</span>      map.clear();<a name="line.1137"></a>
<span class="sourceLineNo">1138</span>    }<a name="line.1138"></a>
<span class="sourceLineNo">1139</span><a name="line.1139"></a>
<span class="sourceLineNo">1140</span>    @Override<a name="line.1140"></a>
<span class="sourceLineNo">1141</span>    public Set&lt;K&gt; keySet() {<a name="line.1141"></a>
<span class="sourceLineNo">1142</span>      return map.keySet();<a name="line.1142"></a>
<span class="sourceLineNo">1143</span>    }<a name="line.1143"></a>
<span class="sourceLineNo">1144</span><a name="line.1144"></a>
<span class="sourceLineNo">1145</span>    @Override<a name="line.1145"></a>
<span class="sourceLineNo">1146</span>    public Multiset&lt;K&gt; keys() {<a name="line.1146"></a>
<span class="sourceLineNo">1147</span>      return Multisets.forSet(map.keySet());<a name="line.1147"></a>
<span class="sourceLineNo">1148</span>    }<a name="line.1148"></a>
<span class="sourceLineNo">1149</span><a name="line.1149"></a>
<span class="sourceLineNo">1150</span>    @Override<a name="line.1150"></a>
<span class="sourceLineNo">1151</span>    public Collection&lt;V&gt; values() {<a name="line.1151"></a>
<span class="sourceLineNo">1152</span>      return map.values();<a name="line.1152"></a>
<span class="sourceLineNo">1153</span>    }<a name="line.1153"></a>
<span class="sourceLineNo">1154</span><a name="line.1154"></a>
<span class="sourceLineNo">1155</span>    @Override<a name="line.1155"></a>
<span class="sourceLineNo">1156</span>    public Set&lt;Entry&lt;K, V&gt;&gt; entries() {<a name="line.1156"></a>
<span class="sourceLineNo">1157</span>      return map.entrySet();<a name="line.1157"></a>
<span class="sourceLineNo">1158</span>    }<a name="line.1158"></a>
<span class="sourceLineNo">1159</span><a name="line.1159"></a>
<span class="sourceLineNo">1160</span>    @Override<a name="line.1160"></a>
<span class="sourceLineNo">1161</span>    public Map&lt;K, Collection&lt;V&gt;&gt; asMap() {<a name="line.1161"></a>
<span class="sourceLineNo">1162</span>      Map&lt;K, Collection&lt;V&gt;&gt; result = asMap;<a name="line.1162"></a>
<span class="sourceLineNo">1163</span>      if (result == null) {<a name="line.1163"></a>
<span class="sourceLineNo">1164</span>        asMap = result = new AsMap();<a name="line.1164"></a>
<span class="sourceLineNo">1165</span>      }<a name="line.1165"></a>
<span class="sourceLineNo">1166</span>      return result;<a name="line.1166"></a>
<span class="sourceLineNo">1167</span>    }<a name="line.1167"></a>
<span class="sourceLineNo">1168</span><a name="line.1168"></a>
<span class="sourceLineNo">1169</span>    @Override public boolean equals(@Nullable Object object) {<a name="line.1169"></a>
<span class="sourceLineNo">1170</span>      if (object == this) {<a name="line.1170"></a>
<span class="sourceLineNo">1171</span>        return true;<a name="line.1171"></a>
<span class="sourceLineNo">1172</span>      }<a name="line.1172"></a>
<span class="sourceLineNo">1173</span>      if (object instanceof Multimap) {<a name="line.1173"></a>
<span class="sourceLineNo">1174</span>        Multimap&lt;?, ?&gt; that = (Multimap&lt;?, ?&gt;) object;<a name="line.1174"></a>
<span class="sourceLineNo">1175</span>        return this.size() == that.size() &amp;&amp; asMap().equals(that.asMap());<a name="line.1175"></a>
<span class="sourceLineNo">1176</span>      }<a name="line.1176"></a>
<span class="sourceLineNo">1177</span>      return false;<a name="line.1177"></a>
<span class="sourceLineNo">1178</span>    }<a name="line.1178"></a>
<span class="sourceLineNo">1179</span><a name="line.1179"></a>
<span class="sourceLineNo">1180</span>    @Override public int hashCode() {<a name="line.1180"></a>
<span class="sourceLineNo">1181</span>      return map.hashCode();<a name="line.1181"></a>
<span class="sourceLineNo">1182</span>    }<a name="line.1182"></a>
<span class="sourceLineNo">1183</span><a name="line.1183"></a>
<span class="sourceLineNo">1184</span>    private static final MapJoiner JOINER<a name="line.1184"></a>
<span class="sourceLineNo">1185</span>        = Joiner.on("], ").withKeyValueSeparator("=[").useForNull("null");<a name="line.1185"></a>
<span class="sourceLineNo">1186</span><a name="line.1186"></a>
<span class="sourceLineNo">1187</span>    @Override public String toString() {<a name="line.1187"></a>
<span class="sourceLineNo">1188</span>      if (map.isEmpty()) {<a name="line.1188"></a>
<span class="sourceLineNo">1189</span>        return "{}";<a name="line.1189"></a>
<span class="sourceLineNo">1190</span>      }<a name="line.1190"></a>
<span class="sourceLineNo">1191</span>      StringBuilder builder<a name="line.1191"></a>
<span class="sourceLineNo">1192</span>          = Collections2.newStringBuilderForCollection(map.size()).append('{');<a name="line.1192"></a>
<span class="sourceLineNo">1193</span>      JOINER.appendTo(builder, map);<a name="line.1193"></a>
<span class="sourceLineNo">1194</span>      return builder.append("]}").toString();<a name="line.1194"></a>
<span class="sourceLineNo">1195</span>    }<a name="line.1195"></a>
<span class="sourceLineNo">1196</span><a name="line.1196"></a>
<span class="sourceLineNo">1197</span>    /** @see MapMultimap#asMap */<a name="line.1197"></a>
<span class="sourceLineNo">1198</span>    class AsMapEntries extends Sets.ImprovedAbstractSet&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; {<a name="line.1198"></a>
<span class="sourceLineNo">1199</span>      @Override public int size() {<a name="line.1199"></a>
<span class="sourceLineNo">1200</span>        return map.size();<a name="line.1200"></a>
<span class="sourceLineNo">1201</span>      }<a name="line.1201"></a>
<span class="sourceLineNo">1202</span><a name="line.1202"></a>
<span class="sourceLineNo">1203</span>      @Override public Iterator&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; iterator() {<a name="line.1203"></a>
<span class="sourceLineNo">1204</span>        return new TransformedIterator&lt;K, Entry&lt;K, Collection&lt;V&gt;&gt;&gt;(map.keySet().iterator()) {<a name="line.1204"></a>
<span class="sourceLineNo">1205</span>          @Override<a name="line.1205"></a>
<span class="sourceLineNo">1206</span>          Entry&lt;K, Collection&lt;V&gt;&gt; transform(final K key) {<a name="line.1206"></a>
<span class="sourceLineNo">1207</span>            return new AbstractMapEntry&lt;K, Collection&lt;V&gt;&gt;() {<a name="line.1207"></a>
<span class="sourceLineNo">1208</span>              @Override<a name="line.1208"></a>
<span class="sourceLineNo">1209</span>              public K getKey() {<a name="line.1209"></a>
<span class="sourceLineNo">1210</span>                return key;<a name="line.1210"></a>
<span class="sourceLineNo">1211</span>              }<a name="line.1211"></a>
<span class="sourceLineNo">1212</span><a name="line.1212"></a>
<span class="sourceLineNo">1213</span>              @Override<a name="line.1213"></a>
<span class="sourceLineNo">1214</span>              public Collection&lt;V&gt; getValue() {<a name="line.1214"></a>
<span class="sourceLineNo">1215</span>                return get(key);<a name="line.1215"></a>
<span class="sourceLineNo">1216</span>              }<a name="line.1216"></a>
<span class="sourceLineNo">1217</span>            };<a name="line.1217"></a>
<span class="sourceLineNo">1218</span>          }<a name="line.1218"></a>
<span class="sourceLineNo">1219</span>        };<a name="line.1219"></a>
<span class="sourceLineNo">1220</span>      }<a name="line.1220"></a>
<span class="sourceLineNo">1221</span><a name="line.1221"></a>
<span class="sourceLineNo">1222</span>      @Override public boolean contains(Object o) {<a name="line.1222"></a>
<span class="sourceLineNo">1223</span>        if (!(o instanceof Entry)) {<a name="line.1223"></a>
<span class="sourceLineNo">1224</span>          return false;<a name="line.1224"></a>
<span class="sourceLineNo">1225</span>        }<a name="line.1225"></a>
<span class="sourceLineNo">1226</span>        Entry&lt;?, ?&gt; entry = (Entry&lt;?, ?&gt;) o;<a name="line.1226"></a>
<span class="sourceLineNo">1227</span>        if (!(entry.getValue() instanceof Set)) {<a name="line.1227"></a>
<span class="sourceLineNo">1228</span>          return false;<a name="line.1228"></a>
<span class="sourceLineNo">1229</span>        }<a name="line.1229"></a>
<span class="sourceLineNo">1230</span>        Set&lt;?&gt; set = (Set&lt;?&gt;) entry.getValue();<a name="line.1230"></a>
<span class="sourceLineNo">1231</span>        return (set.size() == 1)<a name="line.1231"></a>
<span class="sourceLineNo">1232</span>            &amp;&amp; containsEntry(entry.getKey(), set.iterator().next());<a name="line.1232"></a>
<span class="sourceLineNo">1233</span>      }<a name="line.1233"></a>
<span class="sourceLineNo">1234</span><a name="line.1234"></a>
<span class="sourceLineNo">1235</span>      @Override public boolean remove(Object o) {<a name="line.1235"></a>
<span class="sourceLineNo">1236</span>        if (!(o instanceof Entry)) {<a name="line.1236"></a>
<span class="sourceLineNo">1237</span>          return false;<a name="line.1237"></a>
<span class="sourceLineNo">1238</span>        }<a name="line.1238"></a>
<span class="sourceLineNo">1239</span>        Entry&lt;?, ?&gt; entry = (Entry&lt;?, ?&gt;) o;<a name="line.1239"></a>
<span class="sourceLineNo">1240</span>        if (!(entry.getValue() instanceof Set)) {<a name="line.1240"></a>
<span class="sourceLineNo">1241</span>          return false;<a name="line.1241"></a>
<span class="sourceLineNo">1242</span>        }<a name="line.1242"></a>
<span class="sourceLineNo">1243</span>        Set&lt;?&gt; set = (Set&lt;?&gt;) entry.getValue();<a name="line.1243"></a>
<span class="sourceLineNo">1244</span>        return (set.size() == 1)<a name="line.1244"></a>
<span class="sourceLineNo">1245</span>            &amp;&amp; map.entrySet().remove(<a name="line.1245"></a>
<span class="sourceLineNo">1246</span>                Maps.immutableEntry(entry.getKey(), set.iterator().next()));<a name="line.1246"></a>
<span class="sourceLineNo">1247</span>      }<a name="line.1247"></a>
<span class="sourceLineNo">1248</span>    }<a name="line.1248"></a>
<span class="sourceLineNo">1249</span><a name="line.1249"></a>
<span class="sourceLineNo">1250</span>    /** @see MapMultimap#asMap */<a name="line.1250"></a>
<span class="sourceLineNo">1251</span>    class AsMap extends Maps.ImprovedAbstractMap&lt;K, Collection&lt;V&gt;&gt; {<a name="line.1251"></a>
<span class="sourceLineNo">1252</span>      @Override protected Set&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; createEntrySet() {<a name="line.1252"></a>
<span class="sourceLineNo">1253</span>        return new AsMapEntries();<a name="line.1253"></a>
<span class="sourceLineNo">1254</span>      }<a name="line.1254"></a>
<span class="sourceLineNo">1255</span><a name="line.1255"></a>
<span class="sourceLineNo">1256</span>      // The following methods are included for performance.<a name="line.1256"></a>
<span class="sourceLineNo">1257</span><a name="line.1257"></a>
<span class="sourceLineNo">1258</span>      @Override public boolean containsKey(Object key) {<a name="line.1258"></a>
<span class="sourceLineNo">1259</span>        return map.containsKey(key);<a name="line.1259"></a>
<span class="sourceLineNo">1260</span>      }<a name="line.1260"></a>
<span class="sourceLineNo">1261</span><a name="line.1261"></a>
<span class="sourceLineNo">1262</span>      @SuppressWarnings("unchecked")<a name="line.1262"></a>
<span class="sourceLineNo">1263</span>      @Override public Collection&lt;V&gt; get(Object key) {<a name="line.1263"></a>
<span class="sourceLineNo">1264</span>        Collection&lt;V&gt; collection = MapMultimap.this.get((K) key);<a name="line.1264"></a>
<span class="sourceLineNo">1265</span>        return collection.isEmpty() ? null : collection;<a name="line.1265"></a>
<span class="sourceLineNo">1266</span>      }<a name="line.1266"></a>
<span class="sourceLineNo">1267</span><a name="line.1267"></a>
<span class="sourceLineNo">1268</span>      @Override public Collection&lt;V&gt; remove(Object key) {<a name="line.1268"></a>
<span class="sourceLineNo">1269</span>        Collection&lt;V&gt; collection = removeAll(key);<a name="line.1269"></a>
<span class="sourceLineNo">1270</span>        return collection.isEmpty() ? null : collection;<a name="line.1270"></a>
<span class="sourceLineNo">1271</span>      }<a name="line.1271"></a>
<span class="sourceLineNo">1272</span>    }<a name="line.1272"></a>
<span class="sourceLineNo">1273</span>    private static final long serialVersionUID = 7845222491160860175L;<a name="line.1273"></a>
<span class="sourceLineNo">1274</span>  }<a name="line.1274"></a>
<span class="sourceLineNo">1275</span><a name="line.1275"></a>
<span class="sourceLineNo">1276</span>  /**<a name="line.1276"></a>
<span class="sourceLineNo">1277</span>   * Returns a view of a multimap where each value is transformed by a function.<a name="line.1277"></a>
<span class="sourceLineNo">1278</span>   * All other properties of the multimap, such as iteration order, are left<a name="line.1278"></a>
<span class="sourceLineNo">1279</span>   * intact. For example, the code: &lt;pre&gt;   {@code<a name="line.1279"></a>
<span class="sourceLineNo">1280</span>   *<a name="line.1280"></a>
<span class="sourceLineNo">1281</span>   * Multimap&lt;String, Integer&gt; multimap =<a name="line.1281"></a>
<span class="sourceLineNo">1282</span>   *     ImmutableSetMultimap.of("a", 2, "b", -3, "b", -3, "a", 4, "c", 6);<a name="line.1282"></a>
<span class="sourceLineNo">1283</span>   * Function&lt;Integer, String&gt; square = new Function&lt;Integer, String&gt;() {<a name="line.1283"></a>
<span class="sourceLineNo">1284</span>   *     public String apply(Integer in) {<a name="line.1284"></a>
<span class="sourceLineNo">1285</span>   *       return Integer.toString(in * in);<a name="line.1285"></a>
<span class="sourceLineNo">1286</span>   *     }<a name="line.1286"></a>
<span class="sourceLineNo">1287</span>   * };<a name="line.1287"></a>
<span class="sourceLineNo">1288</span>   * Multimap&lt;String, String&gt; transformed =<a name="line.1288"></a>
<span class="sourceLineNo">1289</span>   *     Multimaps.transformValues(multimap, square);<a name="line.1289"></a>
<span class="sourceLineNo">1290</span>   *   System.out.println(transformed);}&lt;/pre&gt;<a name="line.1290"></a>
<span class="sourceLineNo">1291</span>   *<a name="line.1291"></a>
<span class="sourceLineNo">1292</span>   * ... prints {@code {a=[4, 16], b=[9, 9], c=[6]}}.<a name="line.1292"></a>
<span class="sourceLineNo">1293</span>   *<a name="line.1293"></a>
<span class="sourceLineNo">1294</span>   * &lt;p&gt;Changes in the underlying multimap are reflected in this view.<a name="line.1294"></a>
<span class="sourceLineNo">1295</span>   * Conversely, this view supports removal operations, and these are reflected<a name="line.1295"></a>
<span class="sourceLineNo">1296</span>   * in the underlying multimap.<a name="line.1296"></a>
<span class="sourceLineNo">1297</span>   *<a name="line.1297"></a>
<span class="sourceLineNo">1298</span>   * &lt;p&gt;It's acceptable for the underlying multimap to contain null keys, and<a name="line.1298"></a>
<span class="sourceLineNo">1299</span>   * even null values provided that the function is capable of accepting null<a name="line.1299"></a>
<span class="sourceLineNo">1300</span>   * input.  The transformed multimap might contain null values, if the function<a name="line.1300"></a>
<span class="sourceLineNo">1301</span>   * sometimes gives a null result.<a name="line.1301"></a>
<span class="sourceLineNo">1302</span>   *<a name="line.1302"></a>
<span class="sourceLineNo">1303</span>   * &lt;p&gt;The returned multimap is not thread-safe or serializable, even if the<a name="line.1303"></a>
<span class="sourceLineNo">1304</span>   * underlying multimap is.  The {@code equals} and {@code hashCode} methods<a name="line.1304"></a>
<span class="sourceLineNo">1305</span>   * of the returned multimap are meaningless, since there is not a definition<a name="line.1305"></a>
<span class="sourceLineNo">1306</span>   * of {@code equals} or {@code hashCode} for general collections, and<a name="line.1306"></a>
<span class="sourceLineNo">1307</span>   * {@code get()} will return a general {@code Collection} as opposed to a<a name="line.1307"></a>
<span class="sourceLineNo">1308</span>   * {@code List} or a {@code Set}.<a name="line.1308"></a>
<span class="sourceLineNo">1309</span>   *<a name="line.1309"></a>
<span class="sourceLineNo">1310</span>   * &lt;p&gt;The function is applied lazily, invoked when needed. This is necessary<a name="line.1310"></a>
<span class="sourceLineNo">1311</span>   * for the returned multimap to be a view, but it means that the function will<a name="line.1311"></a>
<span class="sourceLineNo">1312</span>   * be applied many times for bulk operations like<a name="line.1312"></a>
<span class="sourceLineNo">1313</span>   * {@link Multimap#containsValue} and {@code Multimap.toString()}. For this to<a name="line.1313"></a>
<span class="sourceLineNo">1314</span>   * perform well, {@code function} should be fast. To avoid lazy evaluation<a name="line.1314"></a>
<span class="sourceLineNo">1315</span>   * when the returned multimap doesn't need to be a view, copy the returned<a name="line.1315"></a>
<span class="sourceLineNo">1316</span>   * multimap into a new multimap of your choosing.<a name="line.1316"></a>
<span class="sourceLineNo">1317</span>   *<a name="line.1317"></a>
<span class="sourceLineNo">1318</span>   * @since 7.0<a name="line.1318"></a>
<span class="sourceLineNo">1319</span>   */<a name="line.1319"></a>
<span class="sourceLineNo">1320</span>  public static &lt;K, V1, V2&gt; Multimap&lt;K, V2&gt; transformValues(<a name="line.1320"></a>
<span class="sourceLineNo">1321</span>      Multimap&lt;K, V1&gt; fromMultimap, final Function&lt;? super V1, V2&gt; function) {<a name="line.1321"></a>
<span class="sourceLineNo">1322</span>    checkNotNull(function);<a name="line.1322"></a>
<span class="sourceLineNo">1323</span>    EntryTransformer&lt;K, V1, V2&gt; transformer =<a name="line.1323"></a>
<span class="sourceLineNo">1324</span>        new EntryTransformer&lt;K, V1, V2&gt;() {<a name="line.1324"></a>
<span class="sourceLineNo">1325</span>          @Override<a name="line.1325"></a>
<span class="sourceLineNo">1326</span>          public V2 transformEntry(K key, V1 value) {<a name="line.1326"></a>
<span class="sourceLineNo">1327</span>            return function.apply(value);<a name="line.1327"></a>
<span class="sourceLineNo">1328</span>          }<a name="line.1328"></a>
<span class="sourceLineNo">1329</span>        };<a name="line.1329"></a>
<span class="sourceLineNo">1330</span>    return transformEntries(fromMultimap, transformer);<a name="line.1330"></a>
<span class="sourceLineNo">1331</span>  }<a name="line.1331"></a>
<span class="sourceLineNo">1332</span><a name="line.1332"></a>
<span class="sourceLineNo">1333</span>  /**<a name="line.1333"></a>
<span class="sourceLineNo">1334</span>   * Returns a view of a multimap whose values are derived from the original<a name="line.1334"></a>
<span class="sourceLineNo">1335</span>   * multimap's entries. In contrast to {@link #transformValues}, this method's<a name="line.1335"></a>
<span class="sourceLineNo">1336</span>   * entry-transformation logic may depend on the key as well as the value.<a name="line.1336"></a>
<span class="sourceLineNo">1337</span>   *<a name="line.1337"></a>
<span class="sourceLineNo">1338</span>   * &lt;p&gt;All other properties of the transformed multimap, such as iteration<a name="line.1338"></a>
<span class="sourceLineNo">1339</span>   * order, are left intact. For example, the code: &lt;pre&gt;   {@code<a name="line.1339"></a>
<span class="sourceLineNo">1340</span>   *<a name="line.1340"></a>
<span class="sourceLineNo">1341</span>   *   SetMultimap&lt;String, Integer&gt; multimap =<a name="line.1341"></a>
<span class="sourceLineNo">1342</span>   *       ImmutableSetMultimap.of("a", 1, "a", 4, "b", -6);<a name="line.1342"></a>
<span class="sourceLineNo">1343</span>   *   EntryTransformer&lt;String, Integer, String&gt; transformer =<a name="line.1343"></a>
<span class="sourceLineNo">1344</span>   *       new EntryTransformer&lt;String, Integer, String&gt;() {<a name="line.1344"></a>
<span class="sourceLineNo">1345</span>   *         public String transformEntry(String key, Integer value) {<a name="line.1345"></a>
<span class="sourceLineNo">1346</span>   *            return (value &gt;= 0) ? key : "no" + key;<a name="line.1346"></a>
<span class="sourceLineNo">1347</span>   *         }<a name="line.1347"></a>
<span class="sourceLineNo">1348</span>   *       };<a name="line.1348"></a>
<span class="sourceLineNo">1349</span>   *   Multimap&lt;String, String&gt; transformed =<a name="line.1349"></a>
<span class="sourceLineNo">1350</span>   *       Multimaps.transformEntries(multimap, transformer);<a name="line.1350"></a>
<span class="sourceLineNo">1351</span>   *   System.out.println(transformed);}&lt;/pre&gt;<a name="line.1351"></a>
<span class="sourceLineNo">1352</span>   *<a name="line.1352"></a>
<span class="sourceLineNo">1353</span>   * ... prints {@code {a=[a, a], b=[nob]}}.<a name="line.1353"></a>
<span class="sourceLineNo">1354</span>   *<a name="line.1354"></a>
<span class="sourceLineNo">1355</span>   * &lt;p&gt;Changes in the underlying multimap are reflected in this view.<a name="line.1355"></a>
<span class="sourceLineNo">1356</span>   * Conversely, this view supports removal operations, and these are reflected<a name="line.1356"></a>
<span class="sourceLineNo">1357</span>   * in the underlying multimap.<a name="line.1357"></a>
<span class="sourceLineNo">1358</span>   *<a name="line.1358"></a>
<span class="sourceLineNo">1359</span>   * &lt;p&gt;It's acceptable for the underlying multimap to contain null keys and<a name="line.1359"></a>
<span class="sourceLineNo">1360</span>   * null values provided that the transformer is capable of accepting null<a name="line.1360"></a>
<span class="sourceLineNo">1361</span>   * inputs. The transformed multimap might contain null values if the<a name="line.1361"></a>
<span class="sourceLineNo">1362</span>   * transformer sometimes gives a null result.<a name="line.1362"></a>
<span class="sourceLineNo">1363</span>   *<a name="line.1363"></a>
<span class="sourceLineNo">1364</span>   * &lt;p&gt;The returned multimap is not thread-safe or serializable, even if the<a name="line.1364"></a>
<span class="sourceLineNo">1365</span>   * underlying multimap is.  The {@code equals} and {@code hashCode} methods<a name="line.1365"></a>
<span class="sourceLineNo">1366</span>   * of the returned multimap are meaningless, since there is not a definition<a name="line.1366"></a>
<span class="sourceLineNo">1367</span>   * of {@code equals} or {@code hashCode} for general collections, and<a name="line.1367"></a>
<span class="sourceLineNo">1368</span>   * {@code get()} will return a general {@code Collection} as opposed to a<a name="line.1368"></a>
<span class="sourceLineNo">1369</span>   * {@code List} or a {@code Set}.<a name="line.1369"></a>
<span class="sourceLineNo">1370</span>   *<a name="line.1370"></a>
<span class="sourceLineNo">1371</span>   * &lt;p&gt;The transformer is applied lazily, invoked when needed. This is<a name="line.1371"></a>
<span class="sourceLineNo">1372</span>   * necessary for the returned multimap to be a view, but it means that the<a name="line.1372"></a>
<span class="sourceLineNo">1373</span>   * transformer will be applied many times for bulk operations like {@link<a name="line.1373"></a>
<span class="sourceLineNo">1374</span>   * Multimap#containsValue} and {@link Object#toString}. For this to perform<a name="line.1374"></a>
<span class="sourceLineNo">1375</span>   * well, {@code transformer} should be fast. To avoid lazy evaluation when the<a name="line.1375"></a>
<span class="sourceLineNo">1376</span>   * returned multimap doesn't need to be a view, copy the returned multimap<a name="line.1376"></a>
<span class="sourceLineNo">1377</span>   * into a new multimap of your choosing.<a name="line.1377"></a>
<span class="sourceLineNo">1378</span>   *<a name="line.1378"></a>
<span class="sourceLineNo">1379</span>   * &lt;p&gt;&lt;b&gt;Warning:&lt;/b&gt; This method assumes that for any instance {@code k} of<a name="line.1379"></a>
<span class="sourceLineNo">1380</span>   * {@code EntryTransformer} key type {@code K}, {@code k.equals(k2)} implies<a name="line.1380"></a>
<span class="sourceLineNo">1381</span>   * that {@code k2} is also of type {@code K}. Using an {@code<a name="line.1381"></a>
<span class="sourceLineNo">1382</span>   * EntryTransformer} key type for which this may not hold, such as {@code<a name="line.1382"></a>
<span class="sourceLineNo">1383</span>   * ArrayList}, may risk a {@code ClassCastException} when calling methods on<a name="line.1383"></a>
<span class="sourceLineNo">1384</span>   * the transformed multimap.<a name="line.1384"></a>
<span class="sourceLineNo">1385</span>   *<a name="line.1385"></a>
<span class="sourceLineNo">1386</span>   * @since 7.0<a name="line.1386"></a>
<span class="sourceLineNo">1387</span>   */<a name="line.1387"></a>
<span class="sourceLineNo">1388</span>  public static &lt;K, V1, V2&gt; Multimap&lt;K, V2&gt; transformEntries(<a name="line.1388"></a>
<span class="sourceLineNo">1389</span>      Multimap&lt;K, V1&gt; fromMap,<a name="line.1389"></a>
<span class="sourceLineNo">1390</span>      EntryTransformer&lt;? super K, ? super V1, V2&gt; transformer) {<a name="line.1390"></a>
<span class="sourceLineNo">1391</span>    return new TransformedEntriesMultimap&lt;K, V1, V2&gt;(fromMap, transformer);<a name="line.1391"></a>
<span class="sourceLineNo">1392</span>  }<a name="line.1392"></a>
<span class="sourceLineNo">1393</span><a name="line.1393"></a>
<span class="sourceLineNo">1394</span>  private static class TransformedEntriesMultimap&lt;K, V1, V2&gt;<a name="line.1394"></a>
<span class="sourceLineNo">1395</span>      implements Multimap&lt;K, V2&gt; {<a name="line.1395"></a>
<span class="sourceLineNo">1396</span>    final Multimap&lt;K, V1&gt; fromMultimap;<a name="line.1396"></a>
<span class="sourceLineNo">1397</span>    final EntryTransformer&lt;? super K, ? super V1, V2&gt; transformer;<a name="line.1397"></a>
<span class="sourceLineNo">1398</span><a name="line.1398"></a>
<span class="sourceLineNo">1399</span>    TransformedEntriesMultimap(Multimap&lt;K, V1&gt; fromMultimap,<a name="line.1399"></a>
<span class="sourceLineNo">1400</span>        final EntryTransformer&lt;? super K, ? super V1, V2&gt; transformer) {<a name="line.1400"></a>
<span class="sourceLineNo">1401</span>      this.fromMultimap = checkNotNull(fromMultimap);<a name="line.1401"></a>
<span class="sourceLineNo">1402</span>      this.transformer = checkNotNull(transformer);<a name="line.1402"></a>
<span class="sourceLineNo">1403</span>    }<a name="line.1403"></a>
<span class="sourceLineNo">1404</span><a name="line.1404"></a>
<span class="sourceLineNo">1405</span>    Collection&lt;V2&gt; transform(final K key, Collection&lt;V1&gt; values) {<a name="line.1405"></a>
<span class="sourceLineNo">1406</span>      return Collections2.transform(values, new Function&lt;V1, V2&gt;() {<a name="line.1406"></a>
<span class="sourceLineNo">1407</span>        @Override public V2 apply(V1 value) {<a name="line.1407"></a>
<span class="sourceLineNo">1408</span>          return transformer.transformEntry(key, value);<a name="line.1408"></a>
<span class="sourceLineNo">1409</span>        }<a name="line.1409"></a>
<span class="sourceLineNo">1410</span>      });<a name="line.1410"></a>
<span class="sourceLineNo">1411</span>    }<a name="line.1411"></a>
<span class="sourceLineNo">1412</span><a name="line.1412"></a>
<span class="sourceLineNo">1413</span>    private transient Map&lt;K, Collection&lt;V2&gt;&gt; asMap;<a name="line.1413"></a>
<span class="sourceLineNo">1414</span><a name="line.1414"></a>
<span class="sourceLineNo">1415</span>    @Override public Map&lt;K, Collection&lt;V2&gt;&gt; asMap() {<a name="line.1415"></a>
<span class="sourceLineNo">1416</span>      if (asMap == null) {<a name="line.1416"></a>
<span class="sourceLineNo">1417</span>        Map&lt;K, Collection&lt;V2&gt;&gt; aM = Maps.transformEntries(fromMultimap.asMap(),<a name="line.1417"></a>
<span class="sourceLineNo">1418</span>            new EntryTransformer&lt;K, Collection&lt;V1&gt;, Collection&lt;V2&gt;&gt;() {<a name="line.1418"></a>
<span class="sourceLineNo">1419</span><a name="line.1419"></a>
<span class="sourceLineNo">1420</span>              @Override public Collection&lt;V2&gt; transformEntry(<a name="line.1420"></a>
<span class="sourceLineNo">1421</span>                  K key, Collection&lt;V1&gt; value) {<a name="line.1421"></a>
<span class="sourceLineNo">1422</span>                return transform(key, value);<a name="line.1422"></a>
<span class="sourceLineNo">1423</span>              }<a name="line.1423"></a>
<span class="sourceLineNo">1424</span>            });<a name="line.1424"></a>
<span class="sourceLineNo">1425</span>        asMap = aM;<a name="line.1425"></a>
<span class="sourceLineNo">1426</span>        return aM;<a name="line.1426"></a>
<span class="sourceLineNo">1427</span>      }<a name="line.1427"></a>
<span class="sourceLineNo">1428</span>      return asMap;<a name="line.1428"></a>
<span class="sourceLineNo">1429</span>    }<a name="line.1429"></a>
<span class="sourceLineNo">1430</span><a name="line.1430"></a>
<span class="sourceLineNo">1431</span>    @Override public void clear() {<a name="line.1431"></a>
<span class="sourceLineNo">1432</span>      fromMultimap.clear();<a name="line.1432"></a>
<span class="sourceLineNo">1433</span>    }<a name="line.1433"></a>
<span class="sourceLineNo">1434</span><a name="line.1434"></a>
<span class="sourceLineNo">1435</span>    @SuppressWarnings("unchecked")<a name="line.1435"></a>
<span class="sourceLineNo">1436</span>    @Override public boolean containsEntry(Object key, Object value) {<a name="line.1436"></a>
<span class="sourceLineNo">1437</span>      Collection&lt;V2&gt; values = get((K) key);<a name="line.1437"></a>
<span class="sourceLineNo">1438</span>      return values.contains(value);<a name="line.1438"></a>
<span class="sourceLineNo">1439</span>    }<a name="line.1439"></a>
<span class="sourceLineNo">1440</span><a name="line.1440"></a>
<span class="sourceLineNo">1441</span>    @Override public boolean containsKey(Object key) {<a name="line.1441"></a>
<span class="sourceLineNo">1442</span>      return fromMultimap.containsKey(key);<a name="line.1442"></a>
<span class="sourceLineNo">1443</span>    }<a name="line.1443"></a>
<span class="sourceLineNo">1444</span><a name="line.1444"></a>
<span class="sourceLineNo">1445</span>    @Override public boolean containsValue(Object value) {<a name="line.1445"></a>
<span class="sourceLineNo">1446</span>      return values().contains(value);<a name="line.1446"></a>
<span class="sourceLineNo">1447</span>    }<a name="line.1447"></a>
<span class="sourceLineNo">1448</span><a name="line.1448"></a>
<span class="sourceLineNo">1449</span>    private transient Collection&lt;Entry&lt;K, V2&gt;&gt; entries;<a name="line.1449"></a>
<span class="sourceLineNo">1450</span><a name="line.1450"></a>
<span class="sourceLineNo">1451</span>    @Override public Collection&lt;Entry&lt;K, V2&gt;&gt; entries() {<a name="line.1451"></a>
<span class="sourceLineNo">1452</span>      if (entries == null) {<a name="line.1452"></a>
<span class="sourceLineNo">1453</span>        Collection&lt;Entry&lt;K, V2&gt;&gt; es = new TransformedEntries(transformer);<a name="line.1453"></a>
<span class="sourceLineNo">1454</span>        entries = es;<a name="line.1454"></a>
<span class="sourceLineNo">1455</span>        return es;<a name="line.1455"></a>
<span class="sourceLineNo">1456</span>      }<a name="line.1456"></a>
<span class="sourceLineNo">1457</span>      return entries;<a name="line.1457"></a>
<span class="sourceLineNo">1458</span>    }<a name="line.1458"></a>
<span class="sourceLineNo">1459</span><a name="line.1459"></a>
<span class="sourceLineNo">1460</span>    private class TransformedEntries<a name="line.1460"></a>
<span class="sourceLineNo">1461</span>        extends TransformedCollection&lt;Entry&lt;K, V1&gt;, Entry&lt;K, V2&gt;&gt; {<a name="line.1461"></a>
<span class="sourceLineNo">1462</span><a name="line.1462"></a>
<span class="sourceLineNo">1463</span>      TransformedEntries(<a name="line.1463"></a>
<span class="sourceLineNo">1464</span>          final EntryTransformer&lt;? super K, ? super V1, V2&gt; transformer) {<a name="line.1464"></a>
<span class="sourceLineNo">1465</span>        super(fromMultimap.entries(),<a name="line.1465"></a>
<span class="sourceLineNo">1466</span>            new Function&lt;Entry&lt;K, V1&gt;, Entry&lt;K, V2&gt;&gt;() {<a name="line.1466"></a>
<span class="sourceLineNo">1467</span>              @Override public Entry&lt;K, V2&gt; apply(final Entry&lt;K, V1&gt; entry) {<a name="line.1467"></a>
<span class="sourceLineNo">1468</span>                return new AbstractMapEntry&lt;K, V2&gt;() {<a name="line.1468"></a>
<span class="sourceLineNo">1469</span><a name="line.1469"></a>
<span class="sourceLineNo">1470</span>                  @Override public K getKey() {<a name="line.1470"></a>
<span class="sourceLineNo">1471</span>                    return entry.getKey();<a name="line.1471"></a>
<span class="sourceLineNo">1472</span>                  }<a name="line.1472"></a>
<span class="sourceLineNo">1473</span><a name="line.1473"></a>
<span class="sourceLineNo">1474</span>                  @Override public V2 getValue() {<a name="line.1474"></a>
<span class="sourceLineNo">1475</span>                    return transformer.transformEntry(<a name="line.1475"></a>
<span class="sourceLineNo">1476</span>                        entry.getKey(), entry.getValue());<a name="line.1476"></a>
<span class="sourceLineNo">1477</span>                  }<a name="line.1477"></a>
<span class="sourceLineNo">1478</span>                };<a name="line.1478"></a>
<span class="sourceLineNo">1479</span>              }<a name="line.1479"></a>
<span class="sourceLineNo">1480</span>            });<a name="line.1480"></a>
<span class="sourceLineNo">1481</span>      }<a name="line.1481"></a>
<span class="sourceLineNo">1482</span><a name="line.1482"></a>
<span class="sourceLineNo">1483</span>      @Override public boolean contains(Object o) {<a name="line.1483"></a>
<span class="sourceLineNo">1484</span>        if (o instanceof Entry) {<a name="line.1484"></a>
<span class="sourceLineNo">1485</span>          Entry&lt;?, ?&gt; entry = (Entry&lt;?, ?&gt;) o;<a name="line.1485"></a>
<span class="sourceLineNo">1486</span>          return containsEntry(entry.getKey(), entry.getValue());<a name="line.1486"></a>
<span class="sourceLineNo">1487</span>        }<a name="line.1487"></a>
<span class="sourceLineNo">1488</span>        return false;<a name="line.1488"></a>
<span class="sourceLineNo">1489</span>      }<a name="line.1489"></a>
<span class="sourceLineNo">1490</span><a name="line.1490"></a>
<span class="sourceLineNo">1491</span>      @SuppressWarnings("unchecked")<a name="line.1491"></a>
<span class="sourceLineNo">1492</span>      @Override public boolean remove(Object o) {<a name="line.1492"></a>
<span class="sourceLineNo">1493</span>        if (o instanceof Entry) {<a name="line.1493"></a>
<span class="sourceLineNo">1494</span>          Entry&lt;?, ?&gt; entry = (Entry&lt;?, ?&gt;) o;<a name="line.1494"></a>
<span class="sourceLineNo">1495</span>          Collection&lt;V2&gt; values = get((K) entry.getKey());<a name="line.1495"></a>
<span class="sourceLineNo">1496</span>          return values.remove(entry.getValue());<a name="line.1496"></a>
<span class="sourceLineNo">1497</span>        }<a name="line.1497"></a>
<span class="sourceLineNo">1498</span>        return false;<a name="line.1498"></a>
<span class="sourceLineNo">1499</span>      }<a name="line.1499"></a>
<span class="sourceLineNo">1500</span><a name="line.1500"></a>
<span class="sourceLineNo">1501</span>    }<a name="line.1501"></a>
<span class="sourceLineNo">1502</span><a name="line.1502"></a>
<span class="sourceLineNo">1503</span>    @Override public Collection&lt;V2&gt; get(final K key) {<a name="line.1503"></a>
<span class="sourceLineNo">1504</span>      return transform(key, fromMultimap.get(key));<a name="line.1504"></a>
<span class="sourceLineNo">1505</span>    }<a name="line.1505"></a>
<span class="sourceLineNo">1506</span><a name="line.1506"></a>
<span class="sourceLineNo">1507</span>    @Override public boolean isEmpty() {<a name="line.1507"></a>
<span class="sourceLineNo">1508</span>      return fromMultimap.isEmpty();<a name="line.1508"></a>
<span class="sourceLineNo">1509</span>    }<a name="line.1509"></a>
<span class="sourceLineNo">1510</span><a name="line.1510"></a>
<span class="sourceLineNo">1511</span>    @Override public Set&lt;K&gt; keySet() {<a name="line.1511"></a>
<span class="sourceLineNo">1512</span>      return fromMultimap.keySet();<a name="line.1512"></a>
<span class="sourceLineNo">1513</span>    }<a name="line.1513"></a>
<span class="sourceLineNo">1514</span><a name="line.1514"></a>
<span class="sourceLineNo">1515</span>    @Override public Multiset&lt;K&gt; keys() {<a name="line.1515"></a>
<span class="sourceLineNo">1516</span>      return fromMultimap.keys();<a name="line.1516"></a>
<span class="sourceLineNo">1517</span>    }<a name="line.1517"></a>
<span class="sourceLineNo">1518</span><a name="line.1518"></a>
<span class="sourceLineNo">1519</span>    @Override public boolean put(K key, V2 value) {<a name="line.1519"></a>
<span class="sourceLineNo">1520</span>      throw new UnsupportedOperationException();<a name="line.1520"></a>
<span class="sourceLineNo">1521</span>    }<a name="line.1521"></a>
<span class="sourceLineNo">1522</span><a name="line.1522"></a>
<span class="sourceLineNo">1523</span>    @Override public boolean putAll(K key, Iterable&lt;? extends V2&gt; values) {<a name="line.1523"></a>
<span class="sourceLineNo">1524</span>      throw new UnsupportedOperationException();<a name="line.1524"></a>
<span class="sourceLineNo">1525</span>    }<a name="line.1525"></a>
<span class="sourceLineNo">1526</span><a name="line.1526"></a>
<span class="sourceLineNo">1527</span>    @Override public boolean putAll(<a name="line.1527"></a>
<span class="sourceLineNo">1528</span>        Multimap&lt;? extends K, ? extends V2&gt; multimap) {<a name="line.1528"></a>
<span class="sourceLineNo">1529</span>      throw new UnsupportedOperationException();<a name="line.1529"></a>
<span class="sourceLineNo">1530</span>    }<a name="line.1530"></a>
<span class="sourceLineNo">1531</span><a name="line.1531"></a>
<span class="sourceLineNo">1532</span>    @SuppressWarnings("unchecked")<a name="line.1532"></a>
<span class="sourceLineNo">1533</span>    @Override public boolean remove(Object key, Object value) {<a name="line.1533"></a>
<span class="sourceLineNo">1534</span>      return get((K) key).remove(value);<a name="line.1534"></a>
<span class="sourceLineNo">1535</span>    }<a name="line.1535"></a>
<span class="sourceLineNo">1536</span><a name="line.1536"></a>
<span class="sourceLineNo">1537</span>    @SuppressWarnings("unchecked")<a name="line.1537"></a>
<span class="sourceLineNo">1538</span>    @Override public Collection&lt;V2&gt; removeAll(Object key) {<a name="line.1538"></a>
<span class="sourceLineNo">1539</span>      return transform((K) key, fromMultimap.removeAll(key));<a name="line.1539"></a>
<span class="sourceLineNo">1540</span>    }<a name="line.1540"></a>
<span class="sourceLineNo">1541</span><a name="line.1541"></a>
<span class="sourceLineNo">1542</span>    @Override public Collection&lt;V2&gt; replaceValues(<a name="line.1542"></a>
<span class="sourceLineNo">1543</span>        K key, Iterable&lt;? extends V2&gt; values) {<a name="line.1543"></a>
<span class="sourceLineNo">1544</span>      throw new UnsupportedOperationException();<a name="line.1544"></a>
<span class="sourceLineNo">1545</span>    }<a name="line.1545"></a>
<span class="sourceLineNo">1546</span><a name="line.1546"></a>
<span class="sourceLineNo">1547</span>    @Override public int size() {<a name="line.1547"></a>
<span class="sourceLineNo">1548</span>      return fromMultimap.size();<a name="line.1548"></a>
<span class="sourceLineNo">1549</span>    }<a name="line.1549"></a>
<span class="sourceLineNo">1550</span><a name="line.1550"></a>
<span class="sourceLineNo">1551</span>    private transient Collection&lt;V2&gt; values;<a name="line.1551"></a>
<span class="sourceLineNo">1552</span><a name="line.1552"></a>
<span class="sourceLineNo">1553</span>    @Override public Collection&lt;V2&gt; values() {<a name="line.1553"></a>
<span class="sourceLineNo">1554</span>      if (values == null) {<a name="line.1554"></a>
<span class="sourceLineNo">1555</span>        Collection&lt;V2&gt; vs = Collections2.transform(<a name="line.1555"></a>
<span class="sourceLineNo">1556</span>            fromMultimap.entries(), new Function&lt;Entry&lt;K, V1&gt;, V2&gt;() {<a name="line.1556"></a>
<span class="sourceLineNo">1557</span><a name="line.1557"></a>
<span class="sourceLineNo">1558</span>              @Override public V2 apply(Entry&lt;K, V1&gt; entry) {<a name="line.1558"></a>
<span class="sourceLineNo">1559</span>                return transformer.transformEntry(<a name="line.1559"></a>
<span class="sourceLineNo">1560</span>                    entry.getKey(), entry.getValue());<a name="line.1560"></a>
<span class="sourceLineNo">1561</span>              }<a name="line.1561"></a>
<span class="sourceLineNo">1562</span>            });<a name="line.1562"></a>
<span class="sourceLineNo">1563</span>        values = vs;<a name="line.1563"></a>
<span class="sourceLineNo">1564</span>        return vs;<a name="line.1564"></a>
<span class="sourceLineNo">1565</span>      }<a name="line.1565"></a>
<span class="sourceLineNo">1566</span>      return values;<a name="line.1566"></a>
<span class="sourceLineNo">1567</span>    }<a name="line.1567"></a>
<span class="sourceLineNo">1568</span><a name="line.1568"></a>
<span class="sourceLineNo">1569</span>    @Override public boolean equals(Object obj) {<a name="line.1569"></a>
<span class="sourceLineNo">1570</span>      if (obj instanceof Multimap) {<a name="line.1570"></a>
<span class="sourceLineNo">1571</span>        Multimap&lt;?, ?&gt; other = (Multimap&lt;?, ?&gt;) obj;<a name="line.1571"></a>
<span class="sourceLineNo">1572</span>        return asMap().equals(other.asMap());<a name="line.1572"></a>
<span class="sourceLineNo">1573</span>      }<a name="line.1573"></a>
<span class="sourceLineNo">1574</span>      return false;<a name="line.1574"></a>
<span class="sourceLineNo">1575</span>    }<a name="line.1575"></a>
<span class="sourceLineNo">1576</span><a name="line.1576"></a>
<span class="sourceLineNo">1577</span>    @Override public int hashCode() {<a name="line.1577"></a>
<span class="sourceLineNo">1578</span>      return asMap().hashCode();<a name="line.1578"></a>
<span class="sourceLineNo">1579</span>    }<a name="line.1579"></a>
<span class="sourceLineNo">1580</span><a name="line.1580"></a>
<span class="sourceLineNo">1581</span>    @Override public String toString() {<a name="line.1581"></a>
<span class="sourceLineNo">1582</span>      return asMap().toString();<a name="line.1582"></a>
<span class="sourceLineNo">1583</span>    }<a name="line.1583"></a>
<span class="sourceLineNo">1584</span>  }<a name="line.1584"></a>
<span class="sourceLineNo">1585</span><a name="line.1585"></a>
<span class="sourceLineNo">1586</span>  /**<a name="line.1586"></a>
<span class="sourceLineNo">1587</span>   * Returns a view of a {@code ListMultimap} where each value is transformed by<a name="line.1587"></a>
<span class="sourceLineNo">1588</span>   * a function. All other properties of the multimap, such as iteration order,<a name="line.1588"></a>
<span class="sourceLineNo">1589</span>   * are left intact. For example, the code: &lt;pre&gt;   {@code<a name="line.1589"></a>
<span class="sourceLineNo">1590</span>   *<a name="line.1590"></a>
<span class="sourceLineNo">1591</span>   *   ListMultimap&lt;String, Integer&gt; multimap<a name="line.1591"></a>
<span class="sourceLineNo">1592</span>   *        = ImmutableListMultimap.of("a", 4, "a", 16, "b", 9);<a name="line.1592"></a>
<span class="sourceLineNo">1593</span>   *   Function&lt;Integer, Double&gt; sqrt =<a name="line.1593"></a>
<span class="sourceLineNo">1594</span>   *       new Function&lt;Integer, Double&gt;() {<a name="line.1594"></a>
<span class="sourceLineNo">1595</span>   *         public Double apply(Integer in) {<a name="line.1595"></a>
<span class="sourceLineNo">1596</span>   *           return Math.sqrt((int) in);<a name="line.1596"></a>
<span class="sourceLineNo">1597</span>   *         }<a name="line.1597"></a>
<span class="sourceLineNo">1598</span>   *       };<a name="line.1598"></a>
<span class="sourceLineNo">1599</span>   *   ListMultimap&lt;String, Double&gt; transformed = Multimaps.transformValues(map,<a name="line.1599"></a>
<span class="sourceLineNo">1600</span>   *       sqrt);<a name="line.1600"></a>
<span class="sourceLineNo">1601</span>   *   System.out.println(transformed);}&lt;/pre&gt;<a name="line.1601"></a>
<span class="sourceLineNo">1602</span>   *<a name="line.1602"></a>
<span class="sourceLineNo">1603</span>   * ... prints {@code {a=[2.0, 4.0], b=[3.0]}}.<a name="line.1603"></a>
<span class="sourceLineNo">1604</span>   *<a name="line.1604"></a>
<span class="sourceLineNo">1605</span>   * &lt;p&gt;Changes in the underlying multimap are reflected in this view.<a name="line.1605"></a>
<span class="sourceLineNo">1606</span>   * Conversely, this view supports removal operations, and these are reflected<a name="line.1606"></a>
<span class="sourceLineNo">1607</span>   * in the underlying multimap.<a name="line.1607"></a>
<span class="sourceLineNo">1608</span>   *<a name="line.1608"></a>
<span class="sourceLineNo">1609</span>   * &lt;p&gt;It's acceptable for the underlying multimap to contain null keys, and<a name="line.1609"></a>
<span class="sourceLineNo">1610</span>   * even null values provided that the function is capable of accepting null<a name="line.1610"></a>
<span class="sourceLineNo">1611</span>   * input.  The transformed multimap might contain null values, if the function<a name="line.1611"></a>
<span class="sourceLineNo">1612</span>   * sometimes gives a null result.<a name="line.1612"></a>
<span class="sourceLineNo">1613</span>   *<a name="line.1613"></a>
<span class="sourceLineNo">1614</span>   * &lt;p&gt;The returned multimap is not thread-safe or serializable, even if the<a name="line.1614"></a>
<span class="sourceLineNo">1615</span>   * underlying multimap is.<a name="line.1615"></a>
<span class="sourceLineNo">1616</span>   *<a name="line.1616"></a>
<span class="sourceLineNo">1617</span>   * &lt;p&gt;The function is applied lazily, invoked when needed. This is necessary<a name="line.1617"></a>
<span class="sourceLineNo">1618</span>   * for the returned multimap to be a view, but it means that the function will<a name="line.1618"></a>
<span class="sourceLineNo">1619</span>   * be applied many times for bulk operations like<a name="line.1619"></a>
<span class="sourceLineNo">1620</span>   * {@link Multimap#containsValue} and {@code Multimap.toString()}. For this to<a name="line.1620"></a>
<span class="sourceLineNo">1621</span>   * perform well, {@code function} should be fast. To avoid lazy evaluation<a name="line.1621"></a>
<span class="sourceLineNo">1622</span>   * when the returned multimap doesn't need to be a view, copy the returned<a name="line.1622"></a>
<span class="sourceLineNo">1623</span>   * multimap into a new multimap of your choosing.<a name="line.1623"></a>
<span class="sourceLineNo">1624</span>   *<a name="line.1624"></a>
<span class="sourceLineNo">1625</span>   * @since 7.0<a name="line.1625"></a>
<span class="sourceLineNo">1626</span>   */<a name="line.1626"></a>
<span class="sourceLineNo">1627</span>  public static &lt;K, V1, V2&gt; ListMultimap&lt;K, V2&gt; transformValues(<a name="line.1627"></a>
<span class="sourceLineNo">1628</span>      ListMultimap&lt;K, V1&gt; fromMultimap,<a name="line.1628"></a>
<span class="sourceLineNo">1629</span>      final Function&lt;? super V1, V2&gt; function) {<a name="line.1629"></a>
<span class="sourceLineNo">1630</span>    checkNotNull(function);<a name="line.1630"></a>
<span class="sourceLineNo">1631</span>    EntryTransformer&lt;K, V1, V2&gt; transformer =<a name="line.1631"></a>
<span class="sourceLineNo">1632</span>        new EntryTransformer&lt;K, V1, V2&gt;() {<a name="line.1632"></a>
<span class="sourceLineNo">1633</span>          @Override<a name="line.1633"></a>
<span class="sourceLineNo">1634</span>          public V2 transformEntry(K key, V1 value) {<a name="line.1634"></a>
<span class="sourceLineNo">1635</span>            return function.apply(value);<a name="line.1635"></a>
<span class="sourceLineNo">1636</span>          }<a name="line.1636"></a>
<span class="sourceLineNo">1637</span>        };<a name="line.1637"></a>
<span class="sourceLineNo">1638</span>    return transformEntries(fromMultimap, transformer);<a name="line.1638"></a>
<span class="sourceLineNo">1639</span>  }<a name="line.1639"></a>
<span class="sourceLineNo">1640</span><a name="line.1640"></a>
<span class="sourceLineNo">1641</span>  /**<a name="line.1641"></a>
<span class="sourceLineNo">1642</span>   * Returns a view of a {@code ListMultimap} whose values are derived from the<a name="line.1642"></a>
<span class="sourceLineNo">1643</span>   * original multimap's entries. In contrast to<a name="line.1643"></a>
<span class="sourceLineNo">1644</span>   * {@link #transformValues(ListMultimap, Function)}, this method's<a name="line.1644"></a>
<span class="sourceLineNo">1645</span>   * entry-transformation logic may depend on the key as well as the value.<a name="line.1645"></a>
<span class="sourceLineNo">1646</span>   *<a name="line.1646"></a>
<span class="sourceLineNo">1647</span>   * &lt;p&gt;All other properties of the transformed multimap, such as iteration<a name="line.1647"></a>
<span class="sourceLineNo">1648</span>   * order, are left intact. For example, the code: &lt;pre&gt;   {@code<a name="line.1648"></a>
<span class="sourceLineNo">1649</span>   *<a name="line.1649"></a>
<span class="sourceLineNo">1650</span>   *   Multimap&lt;String, Integer&gt; multimap =<a name="line.1650"></a>
<span class="sourceLineNo">1651</span>   *       ImmutableMultimap.of("a", 1, "a", 4, "b", 6);<a name="line.1651"></a>
<span class="sourceLineNo">1652</span>   *   EntryTransformer&lt;String, Integer, String&gt; transformer =<a name="line.1652"></a>
<span class="sourceLineNo">1653</span>   *       new EntryTransformer&lt;String, Integer, String&gt;() {<a name="line.1653"></a>
<span class="sourceLineNo">1654</span>   *         public String transformEntry(String key, Integer value) {<a name="line.1654"></a>
<span class="sourceLineNo">1655</span>   *           return key + value;<a name="line.1655"></a>
<span class="sourceLineNo">1656</span>   *         }<a name="line.1656"></a>
<span class="sourceLineNo">1657</span>   *       };<a name="line.1657"></a>
<span class="sourceLineNo">1658</span>   *   Multimap&lt;String, String&gt; transformed =<a name="line.1658"></a>
<span class="sourceLineNo">1659</span>   *       Multimaps.transformEntries(multimap, transformer);<a name="line.1659"></a>
<span class="sourceLineNo">1660</span>   *   System.out.println(transformed);}&lt;/pre&gt;<a name="line.1660"></a>
<span class="sourceLineNo">1661</span>   *<a name="line.1661"></a>
<span class="sourceLineNo">1662</span>   * ... prints {@code {"a"=["a1", "a4"], "b"=["b6"]}}.<a name="line.1662"></a>
<span class="sourceLineNo">1663</span>   *<a name="line.1663"></a>
<span class="sourceLineNo">1664</span>   * &lt;p&gt;Changes in the underlying multimap are reflected in this view.<a name="line.1664"></a>
<span class="sourceLineNo">1665</span>   * Conversely, this view supports removal operations, and these are reflected<a name="line.1665"></a>
<span class="sourceLineNo">1666</span>   * in the underlying multimap.<a name="line.1666"></a>
<span class="sourceLineNo">1667</span>   *<a name="line.1667"></a>
<span class="sourceLineNo">1668</span>   * &lt;p&gt;It's acceptable for the underlying multimap to contain null keys and<a name="line.1668"></a>
<span class="sourceLineNo">1669</span>   * null values provided that the transformer is capable of accepting null<a name="line.1669"></a>
<span class="sourceLineNo">1670</span>   * inputs. The transformed multimap might contain null values if the<a name="line.1670"></a>
<span class="sourceLineNo">1671</span>   * transformer sometimes gives a null result.<a name="line.1671"></a>
<span class="sourceLineNo">1672</span>   *<a name="line.1672"></a>
<span class="sourceLineNo">1673</span>   * &lt;p&gt;The returned multimap is not thread-safe or serializable, even if the<a name="line.1673"></a>
<span class="sourceLineNo">1674</span>   * underlying multimap is.<a name="line.1674"></a>
<span class="sourceLineNo">1675</span>   *<a name="line.1675"></a>
<span class="sourceLineNo">1676</span>   * &lt;p&gt;The transformer is applied lazily, invoked when needed. This is<a name="line.1676"></a>
<span class="sourceLineNo">1677</span>   * necessary for the returned multimap to be a view, but it means that the<a name="line.1677"></a>
<span class="sourceLineNo">1678</span>   * transformer will be applied many times for bulk operations like {@link<a name="line.1678"></a>
<span class="sourceLineNo">1679</span>   * Multimap#containsValue} and {@link Object#toString}. For this to perform<a name="line.1679"></a>
<span class="sourceLineNo">1680</span>   * well, {@code transformer} should be fast. To avoid lazy evaluation when the<a name="line.1680"></a>
<span class="sourceLineNo">1681</span>   * returned multimap doesn't need to be a view, copy the returned multimap<a name="line.1681"></a>
<span class="sourceLineNo">1682</span>   * into a new multimap of your choosing.<a name="line.1682"></a>
<span class="sourceLineNo">1683</span>   *<a name="line.1683"></a>
<span class="sourceLineNo">1684</span>   * &lt;p&gt;&lt;b&gt;Warning:&lt;/b&gt; This method assumes that for any instance {@code k} of<a name="line.1684"></a>
<span class="sourceLineNo">1685</span>   * {@code EntryTransformer} key type {@code K}, {@code k.equals(k2)} implies<a name="line.1685"></a>
<span class="sourceLineNo">1686</span>   * that {@code k2} is also of type {@code K}. Using an {@code<a name="line.1686"></a>
<span class="sourceLineNo">1687</span>   * EntryTransformer} key type for which this may not hold, such as {@code<a name="line.1687"></a>
<span class="sourceLineNo">1688</span>   * ArrayList}, may risk a {@code ClassCastException} when calling methods on<a name="line.1688"></a>
<span class="sourceLineNo">1689</span>   * the transformed multimap.<a name="line.1689"></a>
<span class="sourceLineNo">1690</span>   *<a name="line.1690"></a>
<span class="sourceLineNo">1691</span>   * @since 7.0<a name="line.1691"></a>
<span class="sourceLineNo">1692</span>   */<a name="line.1692"></a>
<span class="sourceLineNo">1693</span>  public static &lt;K, V1, V2&gt; ListMultimap&lt;K, V2&gt; transformEntries(<a name="line.1693"></a>
<span class="sourceLineNo">1694</span>      ListMultimap&lt;K, V1&gt; fromMap,<a name="line.1694"></a>
<span class="sourceLineNo">1695</span>      EntryTransformer&lt;? super K, ? super V1, V2&gt; transformer) {<a name="line.1695"></a>
<span class="sourceLineNo">1696</span>    return new TransformedEntriesListMultimap&lt;K, V1, V2&gt;(fromMap, transformer);<a name="line.1696"></a>
<span class="sourceLineNo">1697</span>  }<a name="line.1697"></a>
<span class="sourceLineNo">1698</span><a name="line.1698"></a>
<span class="sourceLineNo">1699</span>  private static final class TransformedEntriesListMultimap&lt;K, V1, V2&gt;<a name="line.1699"></a>
<span class="sourceLineNo">1700</span>      extends TransformedEntriesMultimap&lt;K, V1, V2&gt;<a name="line.1700"></a>
<span class="sourceLineNo">1701</span>      implements ListMultimap&lt;K, V2&gt; {<a name="line.1701"></a>
<span class="sourceLineNo">1702</span><a name="line.1702"></a>
<span class="sourceLineNo">1703</span>    TransformedEntriesListMultimap(ListMultimap&lt;K, V1&gt; fromMultimap,<a name="line.1703"></a>
<span class="sourceLineNo">1704</span>        EntryTransformer&lt;? super K, ? super V1, V2&gt; transformer) {<a name="line.1704"></a>
<span class="sourceLineNo">1705</span>      super(fromMultimap, transformer);<a name="line.1705"></a>
<span class="sourceLineNo">1706</span>    }<a name="line.1706"></a>
<span class="sourceLineNo">1707</span><a name="line.1707"></a>
<span class="sourceLineNo">1708</span>    @Override List&lt;V2&gt; transform(final K key, Collection&lt;V1&gt; values) {<a name="line.1708"></a>
<span class="sourceLineNo">1709</span>      return Lists.transform((List&lt;V1&gt;) values, new Function&lt;V1, V2&gt;() {<a name="line.1709"></a>
<span class="sourceLineNo">1710</span>        @Override public V2 apply(V1 value) {<a name="line.1710"></a>
<span class="sourceLineNo">1711</span>          return transformer.transformEntry(key, value);<a name="line.1711"></a>
<span class="sourceLineNo">1712</span>        }<a name="line.1712"></a>
<span class="sourceLineNo">1713</span>      });<a name="line.1713"></a>
<span class="sourceLineNo">1714</span>    }<a name="line.1714"></a>
<span class="sourceLineNo">1715</span><a name="line.1715"></a>
<span class="sourceLineNo">1716</span>    @Override public List&lt;V2&gt; get(K key) {<a name="line.1716"></a>
<span class="sourceLineNo">1717</span>      return transform(key, fromMultimap.get(key));<a name="line.1717"></a>
<span class="sourceLineNo">1718</span>    }<a name="line.1718"></a>
<span class="sourceLineNo">1719</span><a name="line.1719"></a>
<span class="sourceLineNo">1720</span>    @SuppressWarnings("unchecked")<a name="line.1720"></a>
<span class="sourceLineNo">1721</span>    @Override public List&lt;V2&gt; removeAll(Object key) {<a name="line.1721"></a>
<span class="sourceLineNo">1722</span>      return transform((K) key, fromMultimap.removeAll(key));<a name="line.1722"></a>
<span class="sourceLineNo">1723</span>    }<a name="line.1723"></a>
<span class="sourceLineNo">1724</span><a name="line.1724"></a>
<span class="sourceLineNo">1725</span>    @Override public List&lt;V2&gt; replaceValues(<a name="line.1725"></a>
<span class="sourceLineNo">1726</span>        K key, Iterable&lt;? extends V2&gt; values) {<a name="line.1726"></a>
<span class="sourceLineNo">1727</span>      throw new UnsupportedOperationException();<a name="line.1727"></a>
<span class="sourceLineNo">1728</span>    }<a name="line.1728"></a>
<span class="sourceLineNo">1729</span>  }<a name="line.1729"></a>
<span class="sourceLineNo">1730</span><a name="line.1730"></a>
<span class="sourceLineNo">1731</span>  /**<a name="line.1731"></a>
<span class="sourceLineNo">1732</span>   * Creates an index {@code ImmutableListMultimap} that contains the results of<a name="line.1732"></a>
<span class="sourceLineNo">1733</span>   * applying a specified function to each item in an {@code Iterable} of<a name="line.1733"></a>
<span class="sourceLineNo">1734</span>   * values. Each value will be stored as a value in the resulting multimap,<a name="line.1734"></a>
<span class="sourceLineNo">1735</span>   * yielding a multimap with the same size as the input iterable. The key used<a name="line.1735"></a>
<span class="sourceLineNo">1736</span>   * to store that value in the multimap will be the result of calling the<a name="line.1736"></a>
<span class="sourceLineNo">1737</span>   * function on that value. The resulting multimap is created as an immutable<a name="line.1737"></a>
<span class="sourceLineNo">1738</span>   * snapshot. In the returned multimap, keys appear in the order they are first<a name="line.1738"></a>
<span class="sourceLineNo">1739</span>   * encountered, and the values corresponding to each key appear in the same<a name="line.1739"></a>
<span class="sourceLineNo">1740</span>   * order as they are encountered.<a name="line.1740"></a>
<span class="sourceLineNo">1741</span>   *<a name="line.1741"></a>
<span class="sourceLineNo">1742</span>   * &lt;p&gt;For example, &lt;pre&gt;   {@code<a name="line.1742"></a>
<span class="sourceLineNo">1743</span>   *<a name="line.1743"></a>
<span class="sourceLineNo">1744</span>   *   List&lt;String&gt; badGuys =<a name="line.1744"></a>
<span class="sourceLineNo">1745</span>   *       Arrays.asList("Inky", "Blinky", "Pinky", "Pinky", "Clyde");<a name="line.1745"></a>
<span class="sourceLineNo">1746</span>   *   Function&lt;String, Integer&gt; stringLengthFunction = ...;<a name="line.1746"></a>
<span class="sourceLineNo">1747</span>   *   Multimap&lt;Integer, String&gt; index =<a name="line.1747"></a>
<span class="sourceLineNo">1748</span>   *       Multimaps.index(badGuys, stringLengthFunction);<a name="line.1748"></a>
<span class="sourceLineNo">1749</span>   *   System.out.println(index);}&lt;/pre&gt;<a name="line.1749"></a>
<span class="sourceLineNo">1750</span>   *<a name="line.1750"></a>
<span class="sourceLineNo">1751</span>   * prints &lt;pre&gt;   {@code<a name="line.1751"></a>
<span class="sourceLineNo">1752</span>   *<a name="line.1752"></a>
<span class="sourceLineNo">1753</span>   *   {4=[Inky], 6=[Blinky], 5=[Pinky, Pinky, Clyde]}}&lt;/pre&gt;<a name="line.1753"></a>
<span class="sourceLineNo">1754</span>   *<a name="line.1754"></a>
<span class="sourceLineNo">1755</span>   * The returned multimap is serializable if its keys and values are all<a name="line.1755"></a>
<span class="sourceLineNo">1756</span>   * serializable.<a name="line.1756"></a>
<span class="sourceLineNo">1757</span>   *<a name="line.1757"></a>
<span class="sourceLineNo">1758</span>   * @param values the values to use when constructing the {@code<a name="line.1758"></a>
<span class="sourceLineNo">1759</span>   *     ImmutableListMultimap}<a name="line.1759"></a>
<span class="sourceLineNo">1760</span>   * @param keyFunction the function used to produce the key for each value<a name="line.1760"></a>
<span class="sourceLineNo">1761</span>   * @return {@code ImmutableListMultimap} mapping the result of evaluating the<a name="line.1761"></a>
<span class="sourceLineNo">1762</span>   *     function {@code keyFunction} on each value in the input collection to<a name="line.1762"></a>
<span class="sourceLineNo">1763</span>   *     that value<a name="line.1763"></a>
<span class="sourceLineNo">1764</span>   * @throws NullPointerException if any of the following cases is true:<a name="line.1764"></a>
<span class="sourceLineNo">1765</span>   *     &lt;ul&gt;<a name="line.1765"></a>
<span class="sourceLineNo">1766</span>   *     &lt;li&gt;{@code values} is null<a name="line.1766"></a>
<span class="sourceLineNo">1767</span>   *     &lt;li&gt;{@code keyFunction} is null<a name="line.1767"></a>
<span class="sourceLineNo">1768</span>   *     &lt;li&gt;An element in {@code values} is null<a name="line.1768"></a>
<span class="sourceLineNo">1769</span>   *     &lt;li&gt;{@code keyFunction} returns {@code null} for any element of {@code<a name="line.1769"></a>
<span class="sourceLineNo">1770</span>   *         values}<a name="line.1770"></a>
<span class="sourceLineNo">1771</span>   *     &lt;/ul&gt;<a name="line.1771"></a>
<span class="sourceLineNo">1772</span>   */<a name="line.1772"></a>
<span class="sourceLineNo">1773</span>  public static &lt;K, V&gt; ImmutableListMultimap&lt;K, V&gt; index(<a name="line.1773"></a>
<span class="sourceLineNo">1774</span>      Iterable&lt;V&gt; values, Function&lt;? super V, K&gt; keyFunction) {<a name="line.1774"></a>
<span class="sourceLineNo">1775</span>    return index(values.iterator(), keyFunction);<a name="line.1775"></a>
<span class="sourceLineNo">1776</span>  }<a name="line.1776"></a>
<span class="sourceLineNo">1777</span><a name="line.1777"></a>
<span class="sourceLineNo">1778</span>  /**<a name="line.1778"></a>
<span class="sourceLineNo">1779</span>   * Creates an index {@code ImmutableListMultimap} that contains the results of<a name="line.1779"></a>
<span class="sourceLineNo">1780</span>   * applying a specified function to each item in an {@code Iterator} of<a name="line.1780"></a>
<span class="sourceLineNo">1781</span>   * values. Each value will be stored as a value in the resulting multimap,<a name="line.1781"></a>
<span class="sourceLineNo">1782</span>   * yielding a multimap with the same size as the input iterator. The key used<a name="line.1782"></a>
<span class="sourceLineNo">1783</span>   * to store that value in the multimap will be the result of calling the<a name="line.1783"></a>
<span class="sourceLineNo">1784</span>   * function on that value. The resulting multimap is created as an immutable<a name="line.1784"></a>
<span class="sourceLineNo">1785</span>   * snapshot. In the returned multimap, keys appear in the order they are first<a name="line.1785"></a>
<span class="sourceLineNo">1786</span>   * encountered, and the values corresponding to each key appear in the same<a name="line.1786"></a>
<span class="sourceLineNo">1787</span>   * order as they are encountered.<a name="line.1787"></a>
<span class="sourceLineNo">1788</span>   *<a name="line.1788"></a>
<span class="sourceLineNo">1789</span>   * &lt;p&gt;For example, &lt;pre&gt;   {@code<a name="line.1789"></a>
<span class="sourceLineNo">1790</span>   *<a name="line.1790"></a>
<span class="sourceLineNo">1791</span>   *   List&lt;String&gt; badGuys =<a name="line.1791"></a>
<span class="sourceLineNo">1792</span>   *       Arrays.asList("Inky", "Blinky", "Pinky", "Pinky", "Clyde");<a name="line.1792"></a>
<span class="sourceLineNo">1793</span>   *   Function&lt;String, Integer&gt; stringLengthFunction = ...;<a name="line.1793"></a>
<span class="sourceLineNo">1794</span>   *   Multimap&lt;Integer, String&gt; index =<a name="line.1794"></a>
<span class="sourceLineNo">1795</span>   *       Multimaps.index(badGuys.iterator(), stringLengthFunction);<a name="line.1795"></a>
<span class="sourceLineNo">1796</span>   *   System.out.println(index);}&lt;/pre&gt;<a name="line.1796"></a>
<span class="sourceLineNo">1797</span>   *<a name="line.1797"></a>
<span class="sourceLineNo">1798</span>   * prints &lt;pre&gt;   {@code<a name="line.1798"></a>
<span class="sourceLineNo">1799</span>   *<a name="line.1799"></a>
<span class="sourceLineNo">1800</span>   *   {4=[Inky], 6=[Blinky], 5=[Pinky, Pinky, Clyde]}}&lt;/pre&gt;<a name="line.1800"></a>
<span class="sourceLineNo">1801</span>   *<a name="line.1801"></a>
<span class="sourceLineNo">1802</span>   * The returned multimap is serializable if its keys and values are all<a name="line.1802"></a>
<span class="sourceLineNo">1803</span>   * serializable.<a name="line.1803"></a>
<span class="sourceLineNo">1804</span>   *<a name="line.1804"></a>
<span class="sourceLineNo">1805</span>   * @param values the values to use when constructing the {@code<a name="line.1805"></a>
<span class="sourceLineNo">1806</span>   *     ImmutableListMultimap}<a name="line.1806"></a>
<span class="sourceLineNo">1807</span>   * @param keyFunction the function used to produce the key for each value<a name="line.1807"></a>
<span class="sourceLineNo">1808</span>   * @return {@code ImmutableListMultimap} mapping the result of evaluating the<a name="line.1808"></a>
<span class="sourceLineNo">1809</span>   *     function {@code keyFunction} on each value in the input collection to<a name="line.1809"></a>
<span class="sourceLineNo">1810</span>   *     that value<a name="line.1810"></a>
<span class="sourceLineNo">1811</span>   * @throws NullPointerException if any of the following cases is true:<a name="line.1811"></a>
<span class="sourceLineNo">1812</span>   *     &lt;ul&gt;<a name="line.1812"></a>
<span class="sourceLineNo">1813</span>   *     &lt;li&gt;{@code values} is null<a name="line.1813"></a>
<span class="sourceLineNo">1814</span>   *     &lt;li&gt;{@code keyFunction} is null<a name="line.1814"></a>
<span class="sourceLineNo">1815</span>   *     &lt;li&gt;An element in {@code values} is null<a name="line.1815"></a>
<span class="sourceLineNo">1816</span>   *     &lt;li&gt;{@code keyFunction} returns {@code null} for any element of {@code<a name="line.1816"></a>
<span class="sourceLineNo">1817</span>   *         values}<a name="line.1817"></a>
<span class="sourceLineNo">1818</span>   *     &lt;/ul&gt;<a name="line.1818"></a>
<span class="sourceLineNo">1819</span>   * @since 10.0<a name="line.1819"></a>
<span class="sourceLineNo">1820</span>   */<a name="line.1820"></a>
<span class="sourceLineNo">1821</span>  public static &lt;K, V&gt; ImmutableListMultimap&lt;K, V&gt; index(<a name="line.1821"></a>
<span class="sourceLineNo">1822</span>      Iterator&lt;V&gt; values, Function&lt;? super V, K&gt; keyFunction) {<a name="line.1822"></a>
<span class="sourceLineNo">1823</span>    checkNotNull(keyFunction);<a name="line.1823"></a>
<span class="sourceLineNo">1824</span>    ImmutableListMultimap.Builder&lt;K, V&gt; builder<a name="line.1824"></a>
<span class="sourceLineNo">1825</span>        = ImmutableListMultimap.builder();<a name="line.1825"></a>
<span class="sourceLineNo">1826</span>    while (values.hasNext()) {<a name="line.1826"></a>
<span class="sourceLineNo">1827</span>      V value = values.next();<a name="line.1827"></a>
<span class="sourceLineNo">1828</span>      checkNotNull(value, values);<a name="line.1828"></a>
<span class="sourceLineNo">1829</span>      builder.put(keyFunction.apply(value), value);<a name="line.1829"></a>
<span class="sourceLineNo">1830</span>    }<a name="line.1830"></a>
<span class="sourceLineNo">1831</span>    return builder.build();<a name="line.1831"></a>
<span class="sourceLineNo">1832</span>  }<a name="line.1832"></a>
<span class="sourceLineNo">1833</span><a name="line.1833"></a>
<span class="sourceLineNo">1834</span>  static abstract class Keys&lt;K, V&gt; extends AbstractMultiset&lt;K&gt; {<a name="line.1834"></a>
<span class="sourceLineNo">1835</span>    abstract Multimap&lt;K, V&gt; multimap();<a name="line.1835"></a>
<span class="sourceLineNo">1836</span><a name="line.1836"></a>
<span class="sourceLineNo">1837</span>    @Override Iterator&lt;Multiset.Entry&lt;K&gt;&gt; entryIterator() {<a name="line.1837"></a>
<span class="sourceLineNo">1838</span>      return new TransformedIterator&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;, Multiset.Entry&lt;K&gt;&gt;(<a name="line.1838"></a>
<span class="sourceLineNo">1839</span>          multimap().asMap().entrySet().iterator()) {<a name="line.1839"></a>
<span class="sourceLineNo">1840</span>        @Override<a name="line.1840"></a>
<span class="sourceLineNo">1841</span>        Multiset.Entry&lt;K&gt; transform(<a name="line.1841"></a>
<span class="sourceLineNo">1842</span>            final Map.Entry&lt;K, Collection&lt;V&gt;&gt; backingEntry) {<a name="line.1842"></a>
<span class="sourceLineNo">1843</span>          return new Multisets.AbstractEntry&lt;K&gt;() {<a name="line.1843"></a>
<span class="sourceLineNo">1844</span>            @Override<a name="line.1844"></a>
<span class="sourceLineNo">1845</span>            public K getElement() {<a name="line.1845"></a>
<span class="sourceLineNo">1846</span>              return backingEntry.getKey();<a name="line.1846"></a>
<span class="sourceLineNo">1847</span>            }<a name="line.1847"></a>
<span class="sourceLineNo">1848</span><a name="line.1848"></a>
<span class="sourceLineNo">1849</span>            @Override<a name="line.1849"></a>
<span class="sourceLineNo">1850</span>            public int getCount() {<a name="line.1850"></a>
<span class="sourceLineNo">1851</span>              return backingEntry.getValue().size();<a name="line.1851"></a>
<span class="sourceLineNo">1852</span>            }<a name="line.1852"></a>
<span class="sourceLineNo">1853</span>          };<a name="line.1853"></a>
<span class="sourceLineNo">1854</span>        }<a name="line.1854"></a>
<span class="sourceLineNo">1855</span>      };<a name="line.1855"></a>
<span class="sourceLineNo">1856</span>    }<a name="line.1856"></a>
<span class="sourceLineNo">1857</span><a name="line.1857"></a>
<span class="sourceLineNo">1858</span>    @Override int distinctElements() {<a name="line.1858"></a>
<span class="sourceLineNo">1859</span>      return multimap().asMap().size();<a name="line.1859"></a>
<span class="sourceLineNo">1860</span>    }<a name="line.1860"></a>
<span class="sourceLineNo">1861</span><a name="line.1861"></a>
<span class="sourceLineNo">1862</span>    @Override Set&lt;Multiset.Entry&lt;K&gt;&gt; createEntrySet() {<a name="line.1862"></a>
<span class="sourceLineNo">1863</span>      return new KeysEntrySet();<a name="line.1863"></a>
<span class="sourceLineNo">1864</span>    }<a name="line.1864"></a>
<span class="sourceLineNo">1865</span><a name="line.1865"></a>
<span class="sourceLineNo">1866</span>    class KeysEntrySet extends Multisets.EntrySet&lt;K&gt; {<a name="line.1866"></a>
<span class="sourceLineNo">1867</span>      @Override Multiset&lt;K&gt; multiset() {<a name="line.1867"></a>
<span class="sourceLineNo">1868</span>        return Keys.this;<a name="line.1868"></a>
<span class="sourceLineNo">1869</span>      }<a name="line.1869"></a>
<span class="sourceLineNo">1870</span><a name="line.1870"></a>
<span class="sourceLineNo">1871</span>      @Override public Iterator&lt;Multiset.Entry&lt;K&gt;&gt; iterator() {<a name="line.1871"></a>
<span class="sourceLineNo">1872</span>        return entryIterator();<a name="line.1872"></a>
<span class="sourceLineNo">1873</span>      }<a name="line.1873"></a>
<span class="sourceLineNo">1874</span><a name="line.1874"></a>
<span class="sourceLineNo">1875</span>      @Override public int size() {<a name="line.1875"></a>
<span class="sourceLineNo">1876</span>        return distinctElements();<a name="line.1876"></a>
<span class="sourceLineNo">1877</span>      }<a name="line.1877"></a>
<span class="sourceLineNo">1878</span><a name="line.1878"></a>
<span class="sourceLineNo">1879</span>      @Override public boolean isEmpty() {<a name="line.1879"></a>
<span class="sourceLineNo">1880</span>        return multimap().isEmpty();<a name="line.1880"></a>
<span class="sourceLineNo">1881</span>      }<a name="line.1881"></a>
<span class="sourceLineNo">1882</span><a name="line.1882"></a>
<span class="sourceLineNo">1883</span>      @Override public boolean contains(@Nullable Object o) {<a name="line.1883"></a>
<span class="sourceLineNo">1884</span>        if (o instanceof Multiset.Entry) {<a name="line.1884"></a>
<span class="sourceLineNo">1885</span>          Multiset.Entry&lt;?&gt; entry = (Multiset.Entry&lt;?&gt;) o;<a name="line.1885"></a>
<span class="sourceLineNo">1886</span>          Collection&lt;V&gt; collection = multimap().asMap().get(entry.getElement());<a name="line.1886"></a>
<span class="sourceLineNo">1887</span>          return collection != null &amp;&amp; collection.size() == entry.getCount();<a name="line.1887"></a>
<span class="sourceLineNo">1888</span>        }<a name="line.1888"></a>
<span class="sourceLineNo">1889</span>        return false;<a name="line.1889"></a>
<span class="sourceLineNo">1890</span>      }<a name="line.1890"></a>
<span class="sourceLineNo">1891</span><a name="line.1891"></a>
<span class="sourceLineNo">1892</span>      @Override public boolean remove(@Nullable Object o) {<a name="line.1892"></a>
<span class="sourceLineNo">1893</span>        if (o instanceof Multiset.Entry) {<a name="line.1893"></a>
<span class="sourceLineNo">1894</span>          Multiset.Entry&lt;?&gt; entry = (Multiset.Entry&lt;?&gt;) o;<a name="line.1894"></a>
<span class="sourceLineNo">1895</span>          Collection&lt;V&gt; collection = multimap().asMap().get(entry.getElement());<a name="line.1895"></a>
<span class="sourceLineNo">1896</span>          if (collection != null &amp;&amp; collection.size() == entry.getCount()) {<a name="line.1896"></a>
<span class="sourceLineNo">1897</span>            collection.clear();<a name="line.1897"></a>
<span class="sourceLineNo">1898</span>            return true;<a name="line.1898"></a>
<span class="sourceLineNo">1899</span>          }<a name="line.1899"></a>
<span class="sourceLineNo">1900</span>        }<a name="line.1900"></a>
<span class="sourceLineNo">1901</span>        return false;<a name="line.1901"></a>
<span class="sourceLineNo">1902</span>      }<a name="line.1902"></a>
<span class="sourceLineNo">1903</span>    }<a name="line.1903"></a>
<span class="sourceLineNo">1904</span><a name="line.1904"></a>
<span class="sourceLineNo">1905</span>    @Override public boolean contains(@Nullable Object element) {<a name="line.1905"></a>
<span class="sourceLineNo">1906</span>      return multimap().containsKey(element);<a name="line.1906"></a>
<span class="sourceLineNo">1907</span>    }<a name="line.1907"></a>
<span class="sourceLineNo">1908</span><a name="line.1908"></a>
<span class="sourceLineNo">1909</span>    @Override public Iterator&lt;K&gt; iterator() {<a name="line.1909"></a>
<span class="sourceLineNo">1910</span>      return Maps.keyIterator(multimap().entries().iterator());<a name="line.1910"></a>
<span class="sourceLineNo">1911</span>    }<a name="line.1911"></a>
<span class="sourceLineNo">1912</span><a name="line.1912"></a>
<span class="sourceLineNo">1913</span>    @Override public int count(@Nullable Object element) {<a name="line.1913"></a>
<span class="sourceLineNo">1914</span>      try {<a name="line.1914"></a>
<span class="sourceLineNo">1915</span>        if (multimap().containsKey(element)) {<a name="line.1915"></a>
<span class="sourceLineNo">1916</span>          Collection&lt;V&gt; values = multimap().asMap().get(element);<a name="line.1916"></a>
<span class="sourceLineNo">1917</span>          return (values == null) ? 0 : values.size();<a name="line.1917"></a>
<span class="sourceLineNo">1918</span>        }<a name="line.1918"></a>
<span class="sourceLineNo">1919</span>        return 0;<a name="line.1919"></a>
<span class="sourceLineNo">1920</span>      } catch (ClassCastException e) {<a name="line.1920"></a>
<span class="sourceLineNo">1921</span>        return 0;<a name="line.1921"></a>
<span class="sourceLineNo">1922</span>      } catch (NullPointerException e) {<a name="line.1922"></a>
<span class="sourceLineNo">1923</span>        return 0;<a name="line.1923"></a>
<span class="sourceLineNo">1924</span>      }<a name="line.1924"></a>
<span class="sourceLineNo">1925</span>    }<a name="line.1925"></a>
<span class="sourceLineNo">1926</span><a name="line.1926"></a>
<span class="sourceLineNo">1927</span>    @Override public int remove(@Nullable Object element, int occurrences) {<a name="line.1927"></a>
<span class="sourceLineNo">1928</span>      checkArgument(occurrences &gt;= 0);<a name="line.1928"></a>
<span class="sourceLineNo">1929</span>      if (occurrences == 0) {<a name="line.1929"></a>
<span class="sourceLineNo">1930</span>        return count(element);<a name="line.1930"></a>
<span class="sourceLineNo">1931</span>      }<a name="line.1931"></a>
<span class="sourceLineNo">1932</span><a name="line.1932"></a>
<span class="sourceLineNo">1933</span>      Collection&lt;V&gt; values;<a name="line.1933"></a>
<span class="sourceLineNo">1934</span>      try {<a name="line.1934"></a>
<span class="sourceLineNo">1935</span>        values = multimap().asMap().get(element);<a name="line.1935"></a>
<span class="sourceLineNo">1936</span>      } catch (ClassCastException e) {<a name="line.1936"></a>
<span class="sourceLineNo">1937</span>        return 0;<a name="line.1937"></a>
<span class="sourceLineNo">1938</span>      } catch (NullPointerException e) {<a name="line.1938"></a>
<span class="sourceLineNo">1939</span>        return 0;<a name="line.1939"></a>
<span class="sourceLineNo">1940</span>      }<a name="line.1940"></a>
<span class="sourceLineNo">1941</span><a name="line.1941"></a>
<span class="sourceLineNo">1942</span>      if (values == null) {<a name="line.1942"></a>
<span class="sourceLineNo">1943</span>        return 0;<a name="line.1943"></a>
<span class="sourceLineNo">1944</span>      }<a name="line.1944"></a>
<span class="sourceLineNo">1945</span><a name="line.1945"></a>
<span class="sourceLineNo">1946</span>      int oldCount = values.size();<a name="line.1946"></a>
<span class="sourceLineNo">1947</span>      if (occurrences &gt;= oldCount) {<a name="line.1947"></a>
<span class="sourceLineNo">1948</span>        values.clear();<a name="line.1948"></a>
<span class="sourceLineNo">1949</span>      } else {<a name="line.1949"></a>
<span class="sourceLineNo">1950</span>        Iterator&lt;V&gt; iterator = values.iterator();<a name="line.1950"></a>
<span class="sourceLineNo">1951</span>        for (int i = 0; i &lt; occurrences; i++) {<a name="line.1951"></a>
<span class="sourceLineNo">1952</span>          iterator.next();<a name="line.1952"></a>
<span class="sourceLineNo">1953</span>          iterator.remove();<a name="line.1953"></a>
<span class="sourceLineNo">1954</span>        }<a name="line.1954"></a>
<span class="sourceLineNo">1955</span>      }<a name="line.1955"></a>
<span class="sourceLineNo">1956</span>      return oldCount;<a name="line.1956"></a>
<span class="sourceLineNo">1957</span>    }<a name="line.1957"></a>
<span class="sourceLineNo">1958</span><a name="line.1958"></a>
<span class="sourceLineNo">1959</span>    @Override public void clear() {<a name="line.1959"></a>
<span class="sourceLineNo">1960</span>      multimap().clear();<a name="line.1960"></a>
<span class="sourceLineNo">1961</span>    }<a name="line.1961"></a>
<span class="sourceLineNo">1962</span><a name="line.1962"></a>
<span class="sourceLineNo">1963</span>    @Override public Set&lt;K&gt; elementSet() {<a name="line.1963"></a>
<span class="sourceLineNo">1964</span>      return multimap().keySet();<a name="line.1964"></a>
<span class="sourceLineNo">1965</span>    }<a name="line.1965"></a>
<span class="sourceLineNo">1966</span>  }<a name="line.1966"></a>
<span class="sourceLineNo">1967</span><a name="line.1967"></a>
<span class="sourceLineNo">1968</span>  static abstract class Values&lt;K, V&gt; extends AbstractCollection&lt;V&gt; {<a name="line.1968"></a>
<span class="sourceLineNo">1969</span>    abstract Multimap&lt;K, V&gt; multimap();<a name="line.1969"></a>
<span class="sourceLineNo">1970</span><a name="line.1970"></a>
<span class="sourceLineNo">1971</span>    @Override public Iterator&lt;V&gt; iterator() {<a name="line.1971"></a>
<span class="sourceLineNo">1972</span>      return Maps.valueIterator(multimap().entries().iterator());<a name="line.1972"></a>
<span class="sourceLineNo">1973</span>    }<a name="line.1973"></a>
<span class="sourceLineNo">1974</span><a name="line.1974"></a>
<span class="sourceLineNo">1975</span>    @Override public int size() {<a name="line.1975"></a>
<span class="sourceLineNo">1976</span>      return multimap().size();<a name="line.1976"></a>
<span class="sourceLineNo">1977</span>    }<a name="line.1977"></a>
<span class="sourceLineNo">1978</span><a name="line.1978"></a>
<span class="sourceLineNo">1979</span>    @Override public boolean contains(@Nullable Object o) {<a name="line.1979"></a>
<span class="sourceLineNo">1980</span>      return multimap().containsValue(o);<a name="line.1980"></a>
<span class="sourceLineNo">1981</span>    }<a name="line.1981"></a>
<span class="sourceLineNo">1982</span><a name="line.1982"></a>
<span class="sourceLineNo">1983</span>    @Override public void clear() {<a name="line.1983"></a>
<span class="sourceLineNo">1984</span>      multimap().clear();<a name="line.1984"></a>
<span class="sourceLineNo">1985</span>    }<a name="line.1985"></a>
<span class="sourceLineNo">1986</span>  }<a name="line.1986"></a>
<span class="sourceLineNo">1987</span><a name="line.1987"></a>
<span class="sourceLineNo">1988</span>  /**<a name="line.1988"></a>
<span class="sourceLineNo">1989</span>   * A skeleton implementation of {@link Multimap#entries()}.<a name="line.1989"></a>
<span class="sourceLineNo">1990</span>   */<a name="line.1990"></a>
<span class="sourceLineNo">1991</span>  static abstract class Entries&lt;K, V&gt; extends<a name="line.1991"></a>
<span class="sourceLineNo">1992</span>      AbstractCollection&lt;Map.Entry&lt;K, V&gt;&gt; {<a name="line.1992"></a>
<span class="sourceLineNo">1993</span>    abstract Multimap&lt;K, V&gt; multimap();<a name="line.1993"></a>
<span class="sourceLineNo">1994</span><a name="line.1994"></a>
<span class="sourceLineNo">1995</span>    @Override public int size() {<a name="line.1995"></a>
<span class="sourceLineNo">1996</span>      return multimap().size();<a name="line.1996"></a>
<span class="sourceLineNo">1997</span>    }<a name="line.1997"></a>
<span class="sourceLineNo">1998</span><a name="line.1998"></a>
<span class="sourceLineNo">1999</span>    @Override public boolean contains(@Nullable Object o) {<a name="line.1999"></a>
<span class="sourceLineNo">2000</span>      if (o instanceof Map.Entry) {<a name="line.2000"></a>
<span class="sourceLineNo">2001</span>        Map.Entry&lt;?, ?&gt; entry = (Map.Entry&lt;?, ?&gt;) o;<a name="line.2001"></a>
<span class="sourceLineNo">2002</span>        return multimap().containsEntry(entry.getKey(), entry.getValue());<a name="line.2002"></a>
<span class="sourceLineNo">2003</span>      }<a name="line.2003"></a>
<span class="sourceLineNo">2004</span>      return false;<a name="line.2004"></a>
<span class="sourceLineNo">2005</span>    }<a name="line.2005"></a>
<span class="sourceLineNo">2006</span><a name="line.2006"></a>
<span class="sourceLineNo">2007</span>    @Override public boolean remove(@Nullable Object o) {<a name="line.2007"></a>
<span class="sourceLineNo">2008</span>      if (o instanceof Map.Entry) {<a name="line.2008"></a>
<span class="sourceLineNo">2009</span>        Map.Entry&lt;?, ?&gt; entry = (Map.Entry&lt;?, ?&gt;) o;<a name="line.2009"></a>
<span class="sourceLineNo">2010</span>        return multimap().remove(entry.getKey(), entry.getValue());<a name="line.2010"></a>
<span class="sourceLineNo">2011</span>      }<a name="line.2011"></a>
<span class="sourceLineNo">2012</span>      return false;<a name="line.2012"></a>
<span class="sourceLineNo">2013</span>    }<a name="line.2013"></a>
<span class="sourceLineNo">2014</span><a name="line.2014"></a>
<span class="sourceLineNo">2015</span>    @Override public void clear() {<a name="line.2015"></a>
<span class="sourceLineNo">2016</span>      multimap().clear();<a name="line.2016"></a>
<span class="sourceLineNo">2017</span>    }<a name="line.2017"></a>
<span class="sourceLineNo">2018</span>  }<a name="line.2018"></a>
<span class="sourceLineNo">2019</span><a name="line.2019"></a>
<span class="sourceLineNo">2020</span>  /**<a name="line.2020"></a>
<span class="sourceLineNo">2021</span>   * A skeleton implementation of {@link SetMultimap#entries()}.<a name="line.2021"></a>
<span class="sourceLineNo">2022</span>   */<a name="line.2022"></a>
<span class="sourceLineNo">2023</span>  static abstract class EntrySet&lt;K, V&gt; extends Entries&lt;K, V&gt; implements<a name="line.2023"></a>
<span class="sourceLineNo">2024</span>      Set&lt;Map.Entry&lt;K, V&gt;&gt; {<a name="line.2024"></a>
<span class="sourceLineNo">2025</span>    @Override public int hashCode() {<a name="line.2025"></a>
<span class="sourceLineNo">2026</span>      return Sets.hashCodeImpl(this);<a name="line.2026"></a>
<span class="sourceLineNo">2027</span>    }<a name="line.2027"></a>
<span class="sourceLineNo">2028</span><a name="line.2028"></a>
<span class="sourceLineNo">2029</span>    @Override public boolean equals(@Nullable Object obj) {<a name="line.2029"></a>
<span class="sourceLineNo">2030</span>      return Sets.equalsImpl(this, obj);<a name="line.2030"></a>
<span class="sourceLineNo">2031</span>    }<a name="line.2031"></a>
<span class="sourceLineNo">2032</span>  }<a name="line.2032"></a>
<span class="sourceLineNo">2033</span><a name="line.2033"></a>
<span class="sourceLineNo">2034</span>  /**<a name="line.2034"></a>
<span class="sourceLineNo">2035</span>   * A skeleton implementation of {@link Multimap#asMap()}.<a name="line.2035"></a>
<span class="sourceLineNo">2036</span>   */<a name="line.2036"></a>
<span class="sourceLineNo">2037</span>  static abstract class AsMap&lt;K, V&gt; extends<a name="line.2037"></a>
<span class="sourceLineNo">2038</span>      Maps.ImprovedAbstractMap&lt;K, Collection&lt;V&gt;&gt; {<a name="line.2038"></a>
<span class="sourceLineNo">2039</span>    abstract Multimap&lt;K, V&gt; multimap();<a name="line.2039"></a>
<span class="sourceLineNo">2040</span><a name="line.2040"></a>
<span class="sourceLineNo">2041</span>    @Override public abstract int size();<a name="line.2041"></a>
<span class="sourceLineNo">2042</span><a name="line.2042"></a>
<span class="sourceLineNo">2043</span>    abstract Iterator&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; entryIterator();<a name="line.2043"></a>
<span class="sourceLineNo">2044</span><a name="line.2044"></a>
<span class="sourceLineNo">2045</span>    @Override protected Set&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; createEntrySet() {<a name="line.2045"></a>
<span class="sourceLineNo">2046</span>      return new EntrySet();<a name="line.2046"></a>
<span class="sourceLineNo">2047</span>    }<a name="line.2047"></a>
<span class="sourceLineNo">2048</span><a name="line.2048"></a>
<span class="sourceLineNo">2049</span>    void removeValuesForKey(Object key){<a name="line.2049"></a>
<span class="sourceLineNo">2050</span>      multimap().removeAll(key);<a name="line.2050"></a>
<span class="sourceLineNo">2051</span>    }<a name="line.2051"></a>
<span class="sourceLineNo">2052</span><a name="line.2052"></a>
<span class="sourceLineNo">2053</span>    class EntrySet extends Maps.EntrySet&lt;K, Collection&lt;V&gt;&gt; {<a name="line.2053"></a>
<span class="sourceLineNo">2054</span>      @Override Map&lt;K, Collection&lt;V&gt;&gt; map() {<a name="line.2054"></a>
<span class="sourceLineNo">2055</span>        return AsMap.this;<a name="line.2055"></a>
<span class="sourceLineNo">2056</span>      }<a name="line.2056"></a>
<span class="sourceLineNo">2057</span><a name="line.2057"></a>
<span class="sourceLineNo">2058</span>      @Override public Iterator&lt;Entry&lt;K, Collection&lt;V&gt;&gt;&gt; iterator() {<a name="line.2058"></a>
<span class="sourceLineNo">2059</span>        return entryIterator();<a name="line.2059"></a>
<span class="sourceLineNo">2060</span>      }<a name="line.2060"></a>
<span class="sourceLineNo">2061</span><a name="line.2061"></a>
<span class="sourceLineNo">2062</span>      @Override public boolean remove(Object o) {<a name="line.2062"></a>
<span class="sourceLineNo">2063</span>        if (!contains(o)) {<a name="line.2063"></a>
<span class="sourceLineNo">2064</span>          return false;<a name="line.2064"></a>
<span class="sourceLineNo">2065</span>        }<a name="line.2065"></a>
<span class="sourceLineNo">2066</span>        Map.Entry&lt;?, ?&gt; entry = (Map.Entry&lt;?, ?&gt;) o;<a name="line.2066"></a>
<span class="sourceLineNo">2067</span>        removeValuesForKey(entry.getKey());<a name="line.2067"></a>
<span class="sourceLineNo">2068</span>        return true;<a name="line.2068"></a>
<span class="sourceLineNo">2069</span>      }<a name="line.2069"></a>
<span class="sourceLineNo">2070</span>    }<a name="line.2070"></a>
<span class="sourceLineNo">2071</span><a name="line.2071"></a>
<span class="sourceLineNo">2072</span>    @SuppressWarnings("unchecked")<a name="line.2072"></a>
<span class="sourceLineNo">2073</span>    @Override public Collection&lt;V&gt; get(Object key) {<a name="line.2073"></a>
<span class="sourceLineNo">2074</span>      return containsKey(key) ? multimap().get((K) key) : null;<a name="line.2074"></a>
<span class="sourceLineNo">2075</span>    }<a name="line.2075"></a>
<span class="sourceLineNo">2076</span><a name="line.2076"></a>
<span class="sourceLineNo">2077</span>    @Override public Collection&lt;V&gt; remove(Object key) {<a name="line.2077"></a>
<span class="sourceLineNo">2078</span>      return containsKey(key) ? multimap().removeAll(key) : null;<a name="line.2078"></a>
<span class="sourceLineNo">2079</span>    }<a name="line.2079"></a>
<span class="sourceLineNo">2080</span><a name="line.2080"></a>
<span class="sourceLineNo">2081</span>    @Override public Set&lt;K&gt; keySet() {<a name="line.2081"></a>
<span class="sourceLineNo">2082</span>      return multimap().keySet();<a name="line.2082"></a>
<span class="sourceLineNo">2083</span>    }<a name="line.2083"></a>
<span class="sourceLineNo">2084</span><a name="line.2084"></a>
<span class="sourceLineNo">2085</span>    @Override public boolean isEmpty() {<a name="line.2085"></a>
<span class="sourceLineNo">2086</span>      return multimap().isEmpty();<a name="line.2086"></a>
<span class="sourceLineNo">2087</span>    }<a name="line.2087"></a>
<span class="sourceLineNo">2088</span><a name="line.2088"></a>
<span class="sourceLineNo">2089</span>    @Override public boolean containsKey(Object key) {<a name="line.2089"></a>
<span class="sourceLineNo">2090</span>      return multimap().containsKey(key);<a name="line.2090"></a>
<span class="sourceLineNo">2091</span>    }<a name="line.2091"></a>
<span class="sourceLineNo">2092</span><a name="line.2092"></a>
<span class="sourceLineNo">2093</span>    @Override public void clear() {<a name="line.2093"></a>
<span class="sourceLineNo">2094</span>      multimap().clear();<a name="line.2094"></a>
<span class="sourceLineNo">2095</span>    }<a name="line.2095"></a>
<span class="sourceLineNo">2096</span>  }<a name="line.2096"></a>
<span class="sourceLineNo">2097</span><a name="line.2097"></a>
<span class="sourceLineNo">2098</span>  /**<a name="line.2098"></a>
<span class="sourceLineNo">2099</span>   * Returns a multimap containing the mappings in {@code unfiltered} whose keys<a name="line.2099"></a>
<span class="sourceLineNo">2100</span>   * satisfy a predicate. The returned multimap is a live view of<a name="line.2100"></a>
<span class="sourceLineNo">2101</span>   * {@code unfiltered}; changes to one affect the other.<a name="line.2101"></a>
<span class="sourceLineNo">2102</span>   *<a name="line.2102"></a>
<span class="sourceLineNo">2103</span>   * &lt;p&gt;The resulting multimap's views have iterators that don't support<a name="line.2103"></a>
<span class="sourceLineNo">2104</span>   * {@code remove()}, but all other methods are supported by the multimap and<a name="line.2104"></a>
<span class="sourceLineNo">2105</span>   * its views. When adding a key that doesn't satisfy the predicate, the<a name="line.2105"></a>
<span class="sourceLineNo">2106</span>   * multimap's {@code put()}, {@code putAll()}, and {@code replaceValues()}<a name="line.2106"></a>
<span class="sourceLineNo">2107</span>   * methods throw an {@link IllegalArgumentException}.<a name="line.2107"></a>
<span class="sourceLineNo">2108</span>   *<a name="line.2108"></a>
<span class="sourceLineNo">2109</span>   * &lt;p&gt;When methods such as {@code removeAll()} and {@code clear()} are called on<a name="line.2109"></a>
<span class="sourceLineNo">2110</span>   * the filtered multimap or its views, only mappings whose keys satisfy the<a name="line.2110"></a>
<span class="sourceLineNo">2111</span>   * filter will be removed from the underlying multimap.<a name="line.2111"></a>
<span class="sourceLineNo">2112</span>   *<a name="line.2112"></a>
<span class="sourceLineNo">2113</span>   * &lt;p&gt;The returned multimap isn't threadsafe or serializable, even if<a name="line.2113"></a>
<span class="sourceLineNo">2114</span>   * {@code unfiltered} is.<a name="line.2114"></a>
<span class="sourceLineNo">2115</span>   *<a name="line.2115"></a>
<span class="sourceLineNo">2116</span>   * &lt;p&gt;Many of the filtered multimap's methods, such as {@code size()}, iterate<a name="line.2116"></a>
<span class="sourceLineNo">2117</span>   * across every key/value mapping in the underlying multimap and determine<a name="line.2117"></a>
<span class="sourceLineNo">2118</span>   * which satisfy the filter. When a live view is &lt;i&gt;not&lt;/i&gt; needed, it may be<a name="line.2118"></a>
<span class="sourceLineNo">2119</span>   * faster to copy the filtered multimap and use the copy.<a name="line.2119"></a>
<span class="sourceLineNo">2120</span>   *<a name="line.2120"></a>
<span class="sourceLineNo">2121</span>   * &lt;p&gt;&lt;b&gt;Warning:&lt;/b&gt; {@code keyPredicate} must be &lt;i&gt;consistent with equals&lt;/i&gt;,<a name="line.2121"></a>
<span class="sourceLineNo">2122</span>   * as documented at {@link Predicate#apply}. Do not provide a predicate such<a name="line.2122"></a>
<span class="sourceLineNo">2123</span>   * as {@code Predicates.instanceOf(ArrayList.class)}, which is inconsistent<a name="line.2123"></a>
<span class="sourceLineNo">2124</span>   * with equals.<a name="line.2124"></a>
<span class="sourceLineNo">2125</span>   *<a name="line.2125"></a>
<span class="sourceLineNo">2126</span>   * @since 11.0<a name="line.2126"></a>
<span class="sourceLineNo">2127</span>   */<a name="line.2127"></a>
<span class="sourceLineNo">2128</span>  @GwtIncompatible(value = "untested")<a name="line.2128"></a>
<span class="sourceLineNo">2129</span>  public static &lt;K, V&gt; Multimap&lt;K, V&gt; filterKeys(<a name="line.2129"></a>
<span class="sourceLineNo">2130</span>      Multimap&lt;K, V&gt; unfiltered, final Predicate&lt;? super K&gt; keyPredicate) {<a name="line.2130"></a>
<span class="sourceLineNo">2131</span>    checkNotNull(keyPredicate);<a name="line.2131"></a>
<span class="sourceLineNo">2132</span>    Predicate&lt;Entry&lt;K, V&gt;&gt; entryPredicate =<a name="line.2132"></a>
<span class="sourceLineNo">2133</span>        new Predicate&lt;Entry&lt;K, V&gt;&gt;() {<a name="line.2133"></a>
<span class="sourceLineNo">2134</span>          @Override<a name="line.2134"></a>
<span class="sourceLineNo">2135</span>          public boolean apply(Entry&lt;K, V&gt; input) {<a name="line.2135"></a>
<span class="sourceLineNo">2136</span>            return keyPredicate.apply(input.getKey());<a name="line.2136"></a>
<span class="sourceLineNo">2137</span>          }<a name="line.2137"></a>
<span class="sourceLineNo">2138</span>        };<a name="line.2138"></a>
<span class="sourceLineNo">2139</span>    return filterEntries(unfiltered, entryPredicate);<a name="line.2139"></a>
<span class="sourceLineNo">2140</span>  }<a name="line.2140"></a>
<span class="sourceLineNo">2141</span><a name="line.2141"></a>
<span class="sourceLineNo">2142</span>  /**<a name="line.2142"></a>
<span class="sourceLineNo">2143</span>   * Returns a multimap containing the mappings in {@code unfiltered} whose values<a name="line.2143"></a>
<span class="sourceLineNo">2144</span>   * satisfy a predicate. The returned multimap is a live view of<a name="line.2144"></a>
<span class="sourceLineNo">2145</span>   * {@code unfiltered}; changes to one affect the other.<a name="line.2145"></a>
<span class="sourceLineNo">2146</span>   *<a name="line.2146"></a>
<span class="sourceLineNo">2147</span>   * &lt;p&gt;The resulting multimap's views have iterators that don't support<a name="line.2147"></a>
<span class="sourceLineNo">2148</span>   * {@code remove()}, but all other methods are supported by the multimap and<a name="line.2148"></a>
<span class="sourceLineNo">2149</span>   * its views. When adding a value that doesn't satisfy the predicate, the<a name="line.2149"></a>
<span class="sourceLineNo">2150</span>   * multimap's {@code put()}, {@code putAll()}, and {@code replaceValues()}<a name="line.2150"></a>
<span class="sourceLineNo">2151</span>   * methods throw an {@link IllegalArgumentException}.<a name="line.2151"></a>
<span class="sourceLineNo">2152</span>   *<a name="line.2152"></a>
<span class="sourceLineNo">2153</span>   * &lt;p&gt;When methods such as {@code removeAll()} and {@code clear()} are called on<a name="line.2153"></a>
<span class="sourceLineNo">2154</span>   * the filtered multimap or its views, only mappings whose value satisfy the<a name="line.2154"></a>
<span class="sourceLineNo">2155</span>   * filter will be removed from the underlying multimap.<a name="line.2155"></a>
<span class="sourceLineNo">2156</span>   *<a name="line.2156"></a>
<span class="sourceLineNo">2157</span>   * &lt;p&gt;The returned multimap isn't threadsafe or serializable, even if<a name="line.2157"></a>
<span class="sourceLineNo">2158</span>   * {@code unfiltered} is.<a name="line.2158"></a>
<span class="sourceLineNo">2159</span>   *<a name="line.2159"></a>
<span class="sourceLineNo">2160</span>   * &lt;p&gt;Many of the filtered multimap's methods, such as {@code size()}, iterate<a name="line.2160"></a>
<span class="sourceLineNo">2161</span>   * across every key/value mapping in the underlying multimap and determine<a name="line.2161"></a>
<span class="sourceLineNo">2162</span>   * which satisfy the filter. When a live view is &lt;i&gt;not&lt;/i&gt; needed, it may be<a name="line.2162"></a>
<span class="sourceLineNo">2163</span>   * faster to copy the filtered multimap and use the copy.<a name="line.2163"></a>
<span class="sourceLineNo">2164</span>   *<a name="line.2164"></a>
<span class="sourceLineNo">2165</span>   * &lt;p&gt;&lt;b&gt;Warning:&lt;/b&gt; {@code valuePredicate} must be &lt;i&gt;consistent with<a name="line.2165"></a>
<span class="sourceLineNo">2166</span>   * equals&lt;/i&gt;, as documented at {@link Predicate#apply}. Do not provide a<a name="line.2166"></a>
<span class="sourceLineNo">2167</span>   * predicate such as {@code Predicates.instanceOf(ArrayList.class)}, which is<a name="line.2167"></a>
<span class="sourceLineNo">2168</span>   * inconsistent with equals.<a name="line.2168"></a>
<span class="sourceLineNo">2169</span>   *<a name="line.2169"></a>
<span class="sourceLineNo">2170</span>   * @since 11.0<a name="line.2170"></a>
<span class="sourceLineNo">2171</span>   */<a name="line.2171"></a>
<span class="sourceLineNo">2172</span>  @GwtIncompatible(value = "untested")<a name="line.2172"></a>
<span class="sourceLineNo">2173</span>  public static &lt;K, V&gt; Multimap&lt;K, V&gt; filterValues(<a name="line.2173"></a>
<span class="sourceLineNo">2174</span>      Multimap&lt;K, V&gt; unfiltered, final Predicate&lt;? super V&gt; valuePredicate) {<a name="line.2174"></a>
<span class="sourceLineNo">2175</span>    checkNotNull(valuePredicate);<a name="line.2175"></a>
<span class="sourceLineNo">2176</span>    Predicate&lt;Entry&lt;K, V&gt;&gt; entryPredicate =<a name="line.2176"></a>
<span class="sourceLineNo">2177</span>        new Predicate&lt;Entry&lt;K, V&gt;&gt;() {<a name="line.2177"></a>
<span class="sourceLineNo">2178</span>          @Override<a name="line.2178"></a>
<span class="sourceLineNo">2179</span>          public boolean apply(Entry&lt;K, V&gt; input) {<a name="line.2179"></a>
<span class="sourceLineNo">2180</span>            return valuePredicate.apply(input.getValue());<a name="line.2180"></a>
<span class="sourceLineNo">2181</span>          }<a name="line.2181"></a>
<span class="sourceLineNo">2182</span>        };<a name="line.2182"></a>
<span class="sourceLineNo">2183</span>    return filterEntries(unfiltered, entryPredicate);<a name="line.2183"></a>
<span class="sourceLineNo">2184</span>  }<a name="line.2184"></a>
<span class="sourceLineNo">2185</span><a name="line.2185"></a>
<span class="sourceLineNo">2186</span>  /**<a name="line.2186"></a>
<span class="sourceLineNo">2187</span>   * Returns a multimap containing the mappings in {@code unfiltered} that<a name="line.2187"></a>
<span class="sourceLineNo">2188</span>   * satisfy a predicate. The returned multimap is a live view of<a name="line.2188"></a>
<span class="sourceLineNo">2189</span>   * {@code unfiltered}; changes to one affect the other.<a name="line.2189"></a>
<span class="sourceLineNo">2190</span>   *<a name="line.2190"></a>
<span class="sourceLineNo">2191</span>   * &lt;p&gt;The resulting multimap's views have iterators that don't support<a name="line.2191"></a>
<span class="sourceLineNo">2192</span>   * {@code remove()}, but all other methods are supported by the multimap and<a name="line.2192"></a>
<span class="sourceLineNo">2193</span>   * its views. When adding a key/value pair that doesn't satisfy the predicate,<a name="line.2193"></a>
<span class="sourceLineNo">2194</span>   * multimap's {@code put()}, {@code putAll()}, and {@code replaceValues()}<a name="line.2194"></a>
<span class="sourceLineNo">2195</span>   * methods throw an {@link IllegalArgumentException}.<a name="line.2195"></a>
<span class="sourceLineNo">2196</span>   *<a name="line.2196"></a>
<span class="sourceLineNo">2197</span>   * &lt;p&gt;When methods such as {@code removeAll()} and {@code clear()} are called on<a name="line.2197"></a>
<span class="sourceLineNo">2198</span>   * the filtered multimap or its views, only mappings whose keys satisfy the<a name="line.2198"></a>
<span class="sourceLineNo">2199</span>   * filter will be removed from the underlying multimap.<a name="line.2199"></a>
<span class="sourceLineNo">2200</span>   *<a name="line.2200"></a>
<span class="sourceLineNo">2201</span>   * &lt;p&gt;The returned multimap isn't threadsafe or serializable, even if<a name="line.2201"></a>
<span class="sourceLineNo">2202</span>   * {@code unfiltered} is.<a name="line.2202"></a>
<span class="sourceLineNo">2203</span>   *<a name="line.2203"></a>
<span class="sourceLineNo">2204</span>   * &lt;p&gt;Many of the filtered multimap's methods, such as {@code size()}, iterate<a name="line.2204"></a>
<span class="sourceLineNo">2205</span>   * across every key/value mapping in the underlying multimap and determine<a name="line.2205"></a>
<span class="sourceLineNo">2206</span>   * which satisfy the filter. When a live view is &lt;i&gt;not&lt;/i&gt; needed, it may be<a name="line.2206"></a>
<span class="sourceLineNo">2207</span>   * faster to copy the filtered multimap and use the copy.<a name="line.2207"></a>
<span class="sourceLineNo">2208</span>   *<a name="line.2208"></a>
<span class="sourceLineNo">2209</span>   * &lt;p&gt;&lt;b&gt;Warning:&lt;/b&gt; {@code entryPredicate} must be &lt;i&gt;consistent with<a name="line.2209"></a>
<span class="sourceLineNo">2210</span>   * equals&lt;/i&gt;, as documented at {@link Predicate#apply}.<a name="line.2210"></a>
<span class="sourceLineNo">2211</span>   *<a name="line.2211"></a>
<span class="sourceLineNo">2212</span>   * @since 11.0<a name="line.2212"></a>
<span class="sourceLineNo">2213</span>   */<a name="line.2213"></a>
<span class="sourceLineNo">2214</span>  @GwtIncompatible(value = "untested")<a name="line.2214"></a>
<span class="sourceLineNo">2215</span>  public static &lt;K, V&gt; Multimap&lt;K, V&gt; filterEntries(<a name="line.2215"></a>
<span class="sourceLineNo">2216</span>      Multimap&lt;K, V&gt; unfiltered, Predicate&lt;? super Entry&lt;K, V&gt;&gt; entryPredicate) {<a name="line.2216"></a>
<span class="sourceLineNo">2217</span>    checkNotNull(entryPredicate);<a name="line.2217"></a>
<span class="sourceLineNo">2218</span>    return (unfiltered instanceof FilteredMultimap)<a name="line.2218"></a>
<span class="sourceLineNo">2219</span>        ? filterFiltered((FilteredMultimap&lt;K, V&gt;) unfiltered, entryPredicate)<a name="line.2219"></a>
<span class="sourceLineNo">2220</span>        : new FilteredMultimap&lt;K, V&gt;(checkNotNull(unfiltered), entryPredicate);<a name="line.2220"></a>
<span class="sourceLineNo">2221</span>  }<a name="line.2221"></a>
<span class="sourceLineNo">2222</span><a name="line.2222"></a>
<span class="sourceLineNo">2223</span>  /**<a name="line.2223"></a>
<span class="sourceLineNo">2224</span>   * Support removal operations when filtering a filtered multimap. Since a<a name="line.2224"></a>
<span class="sourceLineNo">2225</span>   * filtered multimap has iterators that don't support remove, passing one to<a name="line.2225"></a>
<span class="sourceLineNo">2226</span>   * the FilteredMultimap constructor would lead to a multimap whose removal<a name="line.2226"></a>
<span class="sourceLineNo">2227</span>   * operations would fail. This method combines the predicates to avoid that<a name="line.2227"></a>
<span class="sourceLineNo">2228</span>   * problem.<a name="line.2228"></a>
<span class="sourceLineNo">2229</span>   */<a name="line.2229"></a>
<span class="sourceLineNo">2230</span>  private static &lt;K, V&gt; Multimap&lt;K, V&gt; filterFiltered(FilteredMultimap&lt;K, V&gt; map,<a name="line.2230"></a>
<span class="sourceLineNo">2231</span>      Predicate&lt;? super Entry&lt;K, V&gt;&gt; entryPredicate) {<a name="line.2231"></a>
<span class="sourceLineNo">2232</span>    Predicate&lt;Entry&lt;K, V&gt;&gt; predicate<a name="line.2232"></a>
<span class="sourceLineNo">2233</span>        = Predicates.and(map.predicate, entryPredicate);<a name="line.2233"></a>
<span class="sourceLineNo">2234</span>    return new FilteredMultimap&lt;K, V&gt;(map.unfiltered, predicate);<a name="line.2234"></a>
<span class="sourceLineNo">2235</span>  }<a name="line.2235"></a>
<span class="sourceLineNo">2236</span><a name="line.2236"></a>
<span class="sourceLineNo">2237</span>  private static class FilteredMultimap&lt;K, V&gt; implements Multimap&lt;K, V&gt; {<a name="line.2237"></a>
<span class="sourceLineNo">2238</span>    final Multimap&lt;K, V&gt; unfiltered;<a name="line.2238"></a>
<span class="sourceLineNo">2239</span>    final Predicate&lt;? super Entry&lt;K, V&gt;&gt; predicate;<a name="line.2239"></a>
<span class="sourceLineNo">2240</span><a name="line.2240"></a>
<span class="sourceLineNo">2241</span>    FilteredMultimap(Multimap&lt;K, V&gt; unfiltered, Predicate&lt;? super Entry&lt;K, V&gt;&gt; predicate) {<a name="line.2241"></a>
<span class="sourceLineNo">2242</span>      this.unfiltered = unfiltered;<a name="line.2242"></a>
<span class="sourceLineNo">2243</span>      this.predicate = predicate;<a name="line.2243"></a>
<span class="sourceLineNo">2244</span>    }<a name="line.2244"></a>
<span class="sourceLineNo">2245</span><a name="line.2245"></a>
<span class="sourceLineNo">2246</span>    @Override public int size() {<a name="line.2246"></a>
<span class="sourceLineNo">2247</span>      return entries().size();<a name="line.2247"></a>
<span class="sourceLineNo">2248</span>    }<a name="line.2248"></a>
<span class="sourceLineNo">2249</span><a name="line.2249"></a>
<span class="sourceLineNo">2250</span>    @Override public boolean isEmpty() {<a name="line.2250"></a>
<span class="sourceLineNo">2251</span>      return entries().isEmpty();<a name="line.2251"></a>
<span class="sourceLineNo">2252</span>    }<a name="line.2252"></a>
<span class="sourceLineNo">2253</span><a name="line.2253"></a>
<span class="sourceLineNo">2254</span>    @Override public boolean containsKey(Object key) {<a name="line.2254"></a>
<span class="sourceLineNo">2255</span>      return asMap().containsKey(key);<a name="line.2255"></a>
<span class="sourceLineNo">2256</span>    }<a name="line.2256"></a>
<span class="sourceLineNo">2257</span><a name="line.2257"></a>
<span class="sourceLineNo">2258</span>    @Override public boolean containsValue(Object value) {<a name="line.2258"></a>
<span class="sourceLineNo">2259</span>      return values().contains(value);<a name="line.2259"></a>
<span class="sourceLineNo">2260</span>    }<a name="line.2260"></a>
<span class="sourceLineNo">2261</span><a name="line.2261"></a>
<span class="sourceLineNo">2262</span>    // This method should be called only when key is a K and value is a V.<a name="line.2262"></a>
<span class="sourceLineNo">2263</span>    @SuppressWarnings("unchecked")<a name="line.2263"></a>
<span class="sourceLineNo">2264</span>    boolean satisfiesPredicate(Object key, Object value) {<a name="line.2264"></a>
<span class="sourceLineNo">2265</span>      return predicate.apply(Maps.immutableEntry((K) key, (V) value));<a name="line.2265"></a>
<span class="sourceLineNo">2266</span>    }<a name="line.2266"></a>
<span class="sourceLineNo">2267</span><a name="line.2267"></a>
<span class="sourceLineNo">2268</span>    @Override public boolean containsEntry(Object key, Object value) {<a name="line.2268"></a>
<span class="sourceLineNo">2269</span>      return unfiltered.containsEntry(key, value) &amp;&amp; satisfiesPredicate(key, value);<a name="line.2269"></a>
<span class="sourceLineNo">2270</span>    }<a name="line.2270"></a>
<span class="sourceLineNo">2271</span><a name="line.2271"></a>
<span class="sourceLineNo">2272</span>    @Override public boolean put(K key, V value) {<a name="line.2272"></a>
<span class="sourceLineNo">2273</span>      checkArgument(satisfiesPredicate(key, value));<a name="line.2273"></a>
<span class="sourceLineNo">2274</span>      return unfiltered.put(key, value);<a name="line.2274"></a>
<span class="sourceLineNo">2275</span>    }<a name="line.2275"></a>
<span class="sourceLineNo">2276</span><a name="line.2276"></a>
<span class="sourceLineNo">2277</span>    @Override public boolean remove(Object key, Object value) {<a name="line.2277"></a>
<span class="sourceLineNo">2278</span>      return containsEntry(key, value) ? unfiltered.remove(key, value) : false;<a name="line.2278"></a>
<span class="sourceLineNo">2279</span>    }<a name="line.2279"></a>
<span class="sourceLineNo">2280</span><a name="line.2280"></a>
<span class="sourceLineNo">2281</span>    @Override public boolean putAll(K key, Iterable&lt;? extends V&gt; values) {<a name="line.2281"></a>
<span class="sourceLineNo">2282</span>      for (V value : values) {<a name="line.2282"></a>
<span class="sourceLineNo">2283</span>        checkArgument(satisfiesPredicate(key, value));<a name="line.2283"></a>
<span class="sourceLineNo">2284</span>      }<a name="line.2284"></a>
<span class="sourceLineNo">2285</span>      return unfiltered.putAll(key, values);<a name="line.2285"></a>
<span class="sourceLineNo">2286</span>    }<a name="line.2286"></a>
<span class="sourceLineNo">2287</span><a name="line.2287"></a>
<span class="sourceLineNo">2288</span>    @Override public boolean putAll(Multimap&lt;? extends K, ? extends V&gt; multimap) {<a name="line.2288"></a>
<span class="sourceLineNo">2289</span>      for (Entry&lt;? extends K, ? extends V&gt; entry : multimap.entries()) {<a name="line.2289"></a>
<span class="sourceLineNo">2290</span>        checkArgument(satisfiesPredicate(entry.getKey(), entry.getValue()));<a name="line.2290"></a>
<span class="sourceLineNo">2291</span>      }<a name="line.2291"></a>
<span class="sourceLineNo">2292</span>      return unfiltered.putAll(multimap);<a name="line.2292"></a>
<span class="sourceLineNo">2293</span>    }<a name="line.2293"></a>
<span class="sourceLineNo">2294</span><a name="line.2294"></a>
<span class="sourceLineNo">2295</span>    @Override public Collection&lt;V&gt; replaceValues(K key, Iterable&lt;? extends V&gt; values) {<a name="line.2295"></a>
<span class="sourceLineNo">2296</span>      for (V value : values) {<a name="line.2296"></a>
<span class="sourceLineNo">2297</span>        checkArgument(satisfiesPredicate(key, value));<a name="line.2297"></a>
<span class="sourceLineNo">2298</span>      }<a name="line.2298"></a>
<span class="sourceLineNo">2299</span>      // Not calling unfiltered.replaceValues() since values that don't satisify<a name="line.2299"></a>
<span class="sourceLineNo">2300</span>      // the filter should remain in the multimap.<a name="line.2300"></a>
<span class="sourceLineNo">2301</span>      Collection&lt;V&gt; oldValues = removeAll(key);<a name="line.2301"></a>
<span class="sourceLineNo">2302</span>      unfiltered.putAll(key, values);<a name="line.2302"></a>
<span class="sourceLineNo">2303</span>      return oldValues;<a name="line.2303"></a>
<span class="sourceLineNo">2304</span>    }<a name="line.2304"></a>
<span class="sourceLineNo">2305</span><a name="line.2305"></a>
<span class="sourceLineNo">2306</span>    @Override public Collection&lt;V&gt; removeAll(Object key) {<a name="line.2306"></a>
<span class="sourceLineNo">2307</span>      List&lt;V&gt; removed = Lists.newArrayList();<a name="line.2307"></a>
<span class="sourceLineNo">2308</span>      Collection&lt;V&gt; values = unfiltered.asMap().get(key);<a name="line.2308"></a>
<span class="sourceLineNo">2309</span>      if (values != null) {<a name="line.2309"></a>
<span class="sourceLineNo">2310</span>        Iterator&lt;V&gt; iterator = values.iterator();<a name="line.2310"></a>
<span class="sourceLineNo">2311</span>        while (iterator.hasNext()) {<a name="line.2311"></a>
<span class="sourceLineNo">2312</span>          V value = iterator.next();<a name="line.2312"></a>
<span class="sourceLineNo">2313</span>          if (satisfiesPredicate(key, value)) {<a name="line.2313"></a>
<span class="sourceLineNo">2314</span>            removed.add(value);<a name="line.2314"></a>
<span class="sourceLineNo">2315</span>            iterator.remove();<a name="line.2315"></a>
<span class="sourceLineNo">2316</span>          }<a name="line.2316"></a>
<span class="sourceLineNo">2317</span>        }<a name="line.2317"></a>
<span class="sourceLineNo">2318</span>      }<a name="line.2318"></a>
<span class="sourceLineNo">2319</span>      if (unfiltered instanceof SetMultimap) {<a name="line.2319"></a>
<span class="sourceLineNo">2320</span>        return Collections.unmodifiableSet(Sets.newLinkedHashSet(removed));<a name="line.2320"></a>
<span class="sourceLineNo">2321</span>      } else {<a name="line.2321"></a>
<span class="sourceLineNo">2322</span>        return Collections.unmodifiableList(removed);<a name="line.2322"></a>
<span class="sourceLineNo">2323</span>      }<a name="line.2323"></a>
<span class="sourceLineNo">2324</span>    }<a name="line.2324"></a>
<span class="sourceLineNo">2325</span><a name="line.2325"></a>
<span class="sourceLineNo">2326</span>    @Override public void clear() {<a name="line.2326"></a>
<span class="sourceLineNo">2327</span>      entries().clear();<a name="line.2327"></a>
<span class="sourceLineNo">2328</span>    }<a name="line.2328"></a>
<span class="sourceLineNo">2329</span><a name="line.2329"></a>
<span class="sourceLineNo">2330</span>    @Override public boolean equals(@Nullable Object object) {<a name="line.2330"></a>
<span class="sourceLineNo">2331</span>      if (object == this) {<a name="line.2331"></a>
<span class="sourceLineNo">2332</span>        return true;<a name="line.2332"></a>
<span class="sourceLineNo">2333</span>      }<a name="line.2333"></a>
<span class="sourceLineNo">2334</span>      if (object instanceof Multimap) {<a name="line.2334"></a>
<span class="sourceLineNo">2335</span>        Multimap&lt;?, ?&gt; that = (Multimap&lt;?, ?&gt;) object;<a name="line.2335"></a>
<span class="sourceLineNo">2336</span>        return asMap().equals(that.asMap());<a name="line.2336"></a>
<span class="sourceLineNo">2337</span>      }<a name="line.2337"></a>
<span class="sourceLineNo">2338</span>      return false;<a name="line.2338"></a>
<span class="sourceLineNo">2339</span>    }<a name="line.2339"></a>
<span class="sourceLineNo">2340</span><a name="line.2340"></a>
<span class="sourceLineNo">2341</span>    @Override public int hashCode() {<a name="line.2341"></a>
<span class="sourceLineNo">2342</span>      return asMap().hashCode();<a name="line.2342"></a>
<span class="sourceLineNo">2343</span>    }<a name="line.2343"></a>
<span class="sourceLineNo">2344</span><a name="line.2344"></a>
<span class="sourceLineNo">2345</span>    @Override public String toString() {<a name="line.2345"></a>
<span class="sourceLineNo">2346</span>      return asMap().toString();<a name="line.2346"></a>
<span class="sourceLineNo">2347</span>    }<a name="line.2347"></a>
<span class="sourceLineNo">2348</span><a name="line.2348"></a>
<span class="sourceLineNo">2349</span>    class ValuePredicate implements Predicate&lt;V&gt; {<a name="line.2349"></a>
<span class="sourceLineNo">2350</span>      final K key;<a name="line.2350"></a>
<span class="sourceLineNo">2351</span>      ValuePredicate(K key) {<a name="line.2351"></a>
<span class="sourceLineNo">2352</span>        this.key = key;<a name="line.2352"></a>
<span class="sourceLineNo">2353</span>      }<a name="line.2353"></a>
<span class="sourceLineNo">2354</span>      @Override public boolean apply(V value) {<a name="line.2354"></a>
<span class="sourceLineNo">2355</span>        return satisfiesPredicate(key, value);<a name="line.2355"></a>
<span class="sourceLineNo">2356</span>      }<a name="line.2356"></a>
<span class="sourceLineNo">2357</span>    }<a name="line.2357"></a>
<span class="sourceLineNo">2358</span><a name="line.2358"></a>
<span class="sourceLineNo">2359</span>    Collection&lt;V&gt; filterCollection(Collection&lt;V&gt; collection, Predicate&lt;V&gt; predicate) {<a name="line.2359"></a>
<span class="sourceLineNo">2360</span>      if (collection instanceof Set) {<a name="line.2360"></a>
<span class="sourceLineNo">2361</span>        return Sets.filter((Set&lt;V&gt;) collection, predicate);<a name="line.2361"></a>
<span class="sourceLineNo">2362</span>      } else {<a name="line.2362"></a>
<span class="sourceLineNo">2363</span>        return Collections2.filter(collection, predicate);<a name="line.2363"></a>
<span class="sourceLineNo">2364</span>      }<a name="line.2364"></a>
<span class="sourceLineNo">2365</span>    }<a name="line.2365"></a>
<span class="sourceLineNo">2366</span><a name="line.2366"></a>
<span class="sourceLineNo">2367</span>    @Override public Collection&lt;V&gt; get(K key) {<a name="line.2367"></a>
<span class="sourceLineNo">2368</span>      return filterCollection(unfiltered.get(key), new ValuePredicate(key));<a name="line.2368"></a>
<span class="sourceLineNo">2369</span>    }<a name="line.2369"></a>
<span class="sourceLineNo">2370</span><a name="line.2370"></a>
<span class="sourceLineNo">2371</span>    @Override public Set&lt;K&gt; keySet() {<a name="line.2371"></a>
<span class="sourceLineNo">2372</span>      return asMap().keySet();<a name="line.2372"></a>
<span class="sourceLineNo">2373</span>    }<a name="line.2373"></a>
<span class="sourceLineNo">2374</span><a name="line.2374"></a>
<span class="sourceLineNo">2375</span>    Collection&lt;V&gt; values;<a name="line.2375"></a>
<span class="sourceLineNo">2376</span><a name="line.2376"></a>
<span class="sourceLineNo">2377</span>    @Override public Collection&lt;V&gt; values() {<a name="line.2377"></a>
<span class="sourceLineNo">2378</span>      return (values == null) ? values = new Values() : values;<a name="line.2378"></a>
<span class="sourceLineNo">2379</span>    }<a name="line.2379"></a>
<span class="sourceLineNo">2380</span><a name="line.2380"></a>
<span class="sourceLineNo">2381</span>    class Values extends Multimaps.Values&lt;K, V&gt; {<a name="line.2381"></a>
<span class="sourceLineNo">2382</span>      @Override Multimap&lt;K, V&gt; multimap() {<a name="line.2382"></a>
<span class="sourceLineNo">2383</span>        return FilteredMultimap.this;<a name="line.2383"></a>
<span class="sourceLineNo">2384</span>      }<a name="line.2384"></a>
<span class="sourceLineNo">2385</span><a name="line.2385"></a>
<span class="sourceLineNo">2386</span>      @Override public boolean contains(@Nullable Object o) {<a name="line.2386"></a>
<span class="sourceLineNo">2387</span>        return Iterators.contains(iterator(), o);<a name="line.2387"></a>
<span class="sourceLineNo">2388</span>      }<a name="line.2388"></a>
<span class="sourceLineNo">2389</span><a name="line.2389"></a>
<span class="sourceLineNo">2390</span>      // Override remove methods since iterator doesn't support remove.<a name="line.2390"></a>
<span class="sourceLineNo">2391</span><a name="line.2391"></a>
<span class="sourceLineNo">2392</span>      @Override public boolean remove(Object o) {<a name="line.2392"></a>
<span class="sourceLineNo">2393</span>        Iterator&lt;Entry&lt;K, V&gt;&gt; iterator = unfiltered.entries().iterator();<a name="line.2393"></a>
<span class="sourceLineNo">2394</span>        while (iterator.hasNext()) {<a name="line.2394"></a>
<span class="sourceLineNo">2395</span>          Entry&lt;K, V&gt; entry = iterator.next();<a name="line.2395"></a>
<span class="sourceLineNo">2396</span>          if (Objects.equal(o, entry.getValue()) &amp;&amp; predicate.apply(entry)) {<a name="line.2396"></a>
<span class="sourceLineNo">2397</span>            iterator.remove();<a name="line.2397"></a>
<span class="sourceLineNo">2398</span>            return true;<a name="line.2398"></a>
<span class="sourceLineNo">2399</span>          }<a name="line.2399"></a>
<span class="sourceLineNo">2400</span>        }<a name="line.2400"></a>
<span class="sourceLineNo">2401</span>        return false;<a name="line.2401"></a>
<span class="sourceLineNo">2402</span>      }<a name="line.2402"></a>
<span class="sourceLineNo">2403</span><a name="line.2403"></a>
<span class="sourceLineNo">2404</span>      @Override public boolean removeAll(Collection&lt;?&gt; c) {<a name="line.2404"></a>
<span class="sourceLineNo">2405</span>        boolean changed = false;<a name="line.2405"></a>
<span class="sourceLineNo">2406</span>        Iterator&lt;Entry&lt;K, V&gt;&gt; iterator = unfiltered.entries().iterator();<a name="line.2406"></a>
<span class="sourceLineNo">2407</span>        while (iterator.hasNext()) {<a name="line.2407"></a>
<span class="sourceLineNo">2408</span>          Entry&lt;K, V&gt; entry = iterator.next();<a name="line.2408"></a>
<span class="sourceLineNo">2409</span>          if (c.contains(entry.getValue()) &amp;&amp; predicate.apply(entry)) {<a name="line.2409"></a>
<span class="sourceLineNo">2410</span>            iterator.remove();<a name="line.2410"></a>
<span class="sourceLineNo">2411</span>            changed = true;<a name="line.2411"></a>
<span class="sourceLineNo">2412</span>          }<a name="line.2412"></a>
<span class="sourceLineNo">2413</span>        }<a name="line.2413"></a>
<span class="sourceLineNo">2414</span>        return changed;<a name="line.2414"></a>
<span class="sourceLineNo">2415</span>      }<a name="line.2415"></a>
<span class="sourceLineNo">2416</span><a name="line.2416"></a>
<span class="sourceLineNo">2417</span>      @Override public boolean retainAll(Collection&lt;?&gt; c) {<a name="line.2417"></a>
<span class="sourceLineNo">2418</span>        boolean changed = false;<a name="line.2418"></a>
<span class="sourceLineNo">2419</span>        Iterator&lt;Entry&lt;K, V&gt;&gt; iterator = unfiltered.entries().iterator();<a name="line.2419"></a>
<span class="sourceLineNo">2420</span>        while (iterator.hasNext()) {<a name="line.2420"></a>
<span class="sourceLineNo">2421</span>          Entry&lt;K, V&gt; entry = iterator.next();<a name="line.2421"></a>
<span class="sourceLineNo">2422</span>          if (!c.contains(entry.getValue()) &amp;&amp; predicate.apply(entry)) {<a name="line.2422"></a>
<span class="sourceLineNo">2423</span>            iterator.remove();<a name="line.2423"></a>
<span class="sourceLineNo">2424</span>            changed = true;<a name="line.2424"></a>
<span class="sourceLineNo">2425</span>          }<a name="line.2425"></a>
<span class="sourceLineNo">2426</span>        }<a name="line.2426"></a>
<span class="sourceLineNo">2427</span>        return changed;<a name="line.2427"></a>
<span class="sourceLineNo">2428</span>      }<a name="line.2428"></a>
<span class="sourceLineNo">2429</span>    }<a name="line.2429"></a>
<span class="sourceLineNo">2430</span><a name="line.2430"></a>
<span class="sourceLineNo">2431</span>    Collection&lt;Entry&lt;K, V&gt;&gt; entries;<a name="line.2431"></a>
<span class="sourceLineNo">2432</span><a name="line.2432"></a>
<span class="sourceLineNo">2433</span>    @Override public Collection&lt;Entry&lt;K, V&gt;&gt; entries() {<a name="line.2433"></a>
<span class="sourceLineNo">2434</span>      return (entries == null)<a name="line.2434"></a>
<span class="sourceLineNo">2435</span>          ? entries = Collections2.filter(unfiltered.entries(), predicate)<a name="line.2435"></a>
<span class="sourceLineNo">2436</span>          : entries;<a name="line.2436"></a>
<span class="sourceLineNo">2437</span>    }<a name="line.2437"></a>
<span class="sourceLineNo">2438</span><a name="line.2438"></a>
<span class="sourceLineNo">2439</span>    /**<a name="line.2439"></a>
<span class="sourceLineNo">2440</span>     * Remove all filtered asMap() entries that satisfy the predicate.<a name="line.2440"></a>
<span class="sourceLineNo">2441</span>     */<a name="line.2441"></a>
<span class="sourceLineNo">2442</span>    boolean removeEntriesIf(Predicate&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt; removalPredicate) {<a name="line.2442"></a>
<span class="sourceLineNo">2443</span>      Iterator&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt; iterator = unfiltered.asMap().entrySet().iterator();<a name="line.2443"></a>
<span class="sourceLineNo">2444</span>      boolean changed = false;<a name="line.2444"></a>
<span class="sourceLineNo">2445</span>      while (iterator.hasNext()) {<a name="line.2445"></a>
<span class="sourceLineNo">2446</span>        // Determine whether to remove the filtered values with this key.<a name="line.2446"></a>
<span class="sourceLineNo">2447</span>        Map.Entry&lt;K, Collection&lt;V&gt;&gt; entry = iterator.next();<a name="line.2447"></a>
<span class="sourceLineNo">2448</span>        K key = entry.getKey();<a name="line.2448"></a>
<span class="sourceLineNo">2449</span>        Collection&lt;V&gt; collection = entry.getValue();<a name="line.2449"></a>
<span class="sourceLineNo">2450</span>        Predicate&lt;V&gt; valuePredicate = new ValuePredicate(key);<a name="line.2450"></a>
<span class="sourceLineNo">2451</span>        Collection&lt;V&gt; filteredCollection = filterCollection(collection, valuePredicate);<a name="line.2451"></a>
<span class="sourceLineNo">2452</span>        Map.Entry&lt;K, Collection&lt;V&gt;&gt; filteredEntry = Maps.immutableEntry(key, filteredCollection);<a name="line.2452"></a>
<span class="sourceLineNo">2453</span>        if (removalPredicate.apply(filteredEntry) &amp;&amp; !filteredCollection.isEmpty()) {<a name="line.2453"></a>
<span class="sourceLineNo">2454</span>          changed = true;<a name="line.2454"></a>
<span class="sourceLineNo">2455</span>          if (Iterables.all(collection, valuePredicate)) {<a name="line.2455"></a>
<span class="sourceLineNo">2456</span>            iterator.remove();  // Remove all values for the key.<a name="line.2456"></a>
<span class="sourceLineNo">2457</span>          } else {<a name="line.2457"></a>
<span class="sourceLineNo">2458</span>            filteredCollection.clear();  // Remove the filtered values only.<a name="line.2458"></a>
<span class="sourceLineNo">2459</span>          }<a name="line.2459"></a>
<span class="sourceLineNo">2460</span>        }<a name="line.2460"></a>
<span class="sourceLineNo">2461</span>      }<a name="line.2461"></a>
<span class="sourceLineNo">2462</span>      return changed;<a name="line.2462"></a>
<span class="sourceLineNo">2463</span>    }<a name="line.2463"></a>
<span class="sourceLineNo">2464</span><a name="line.2464"></a>
<span class="sourceLineNo">2465</span>    Map&lt;K, Collection&lt;V&gt;&gt; asMap;<a name="line.2465"></a>
<span class="sourceLineNo">2466</span><a name="line.2466"></a>
<span class="sourceLineNo">2467</span>    @Override public Map&lt;K, Collection&lt;V&gt;&gt; asMap() {<a name="line.2467"></a>
<span class="sourceLineNo">2468</span>      return (asMap == null) ? asMap = createAsMap() : asMap;<a name="line.2468"></a>
<span class="sourceLineNo">2469</span>    }<a name="line.2469"></a>
<span class="sourceLineNo">2470</span><a name="line.2470"></a>
<span class="sourceLineNo">2471</span>    static final Predicate&lt;Collection&lt;?&gt;&gt; NOT_EMPTY = new Predicate&lt;Collection&lt;?&gt;&gt;() {<a name="line.2471"></a>
<span class="sourceLineNo">2472</span>      @Override public boolean apply(Collection&lt;?&gt; input) {<a name="line.2472"></a>
<span class="sourceLineNo">2473</span>        return !input.isEmpty();<a name="line.2473"></a>
<span class="sourceLineNo">2474</span>      }<a name="line.2474"></a>
<span class="sourceLineNo">2475</span>    };<a name="line.2475"></a>
<span class="sourceLineNo">2476</span><a name="line.2476"></a>
<span class="sourceLineNo">2477</span>    Map&lt;K, Collection&lt;V&gt;&gt; createAsMap() {<a name="line.2477"></a>
<span class="sourceLineNo">2478</span>      // Select the values that satisify the predicate.<a name="line.2478"></a>
<span class="sourceLineNo">2479</span>      EntryTransformer&lt;K, Collection&lt;V&gt;, Collection&lt;V&gt;&gt; transformer<a name="line.2479"></a>
<span class="sourceLineNo">2480</span>          = new EntryTransformer&lt;K, Collection&lt;V&gt;, Collection&lt;V&gt;&gt;() {<a name="line.2480"></a>
<span class="sourceLineNo">2481</span>            @Override public Collection&lt;V&gt; transformEntry(K key, Collection&lt;V&gt; collection) {<a name="line.2481"></a>
<span class="sourceLineNo">2482</span>              return filterCollection(collection, new ValuePredicate(key));<a name="line.2482"></a>
<span class="sourceLineNo">2483</span>            }<a name="line.2483"></a>
<span class="sourceLineNo">2484</span>      };<a name="line.2484"></a>
<span class="sourceLineNo">2485</span>      Map&lt;K, Collection&lt;V&gt;&gt; transformed<a name="line.2485"></a>
<span class="sourceLineNo">2486</span>          = Maps.transformEntries(unfiltered.asMap(), transformer);<a name="line.2486"></a>
<span class="sourceLineNo">2487</span><a name="line.2487"></a>
<span class="sourceLineNo">2488</span>      // Select the keys that have at least one value remaining.<a name="line.2488"></a>
<span class="sourceLineNo">2489</span>      Map&lt;K, Collection&lt;V&gt;&gt; filtered = Maps.filterValues(transformed, NOT_EMPTY);<a name="line.2489"></a>
<span class="sourceLineNo">2490</span><a name="line.2490"></a>
<span class="sourceLineNo">2491</span>      // Override the removal methods, since removing a map entry should not<a name="line.2491"></a>
<span class="sourceLineNo">2492</span>      // affect values that don't satisfy the filter.<a name="line.2492"></a>
<span class="sourceLineNo">2493</span>      return new AsMap(filtered);<a name="line.2493"></a>
<span class="sourceLineNo">2494</span>    }<a name="line.2494"></a>
<span class="sourceLineNo">2495</span><a name="line.2495"></a>
<span class="sourceLineNo">2496</span>    class AsMap extends ForwardingMap&lt;K, Collection&lt;V&gt;&gt; {<a name="line.2496"></a>
<span class="sourceLineNo">2497</span>     final Map&lt;K, Collection&lt;V&gt;&gt; delegate;<a name="line.2497"></a>
<span class="sourceLineNo">2498</span><a name="line.2498"></a>
<span class="sourceLineNo">2499</span>      AsMap(Map&lt;K, Collection&lt;V&gt;&gt; delegate) {<a name="line.2499"></a>
<span class="sourceLineNo">2500</span>        this.delegate = delegate;<a name="line.2500"></a>
<span class="sourceLineNo">2501</span>      }<a name="line.2501"></a>
<span class="sourceLineNo">2502</span><a name="line.2502"></a>
<span class="sourceLineNo">2503</span>      @Override protected Map&lt;K, Collection&lt;V&gt;&gt; delegate() {<a name="line.2503"></a>
<span class="sourceLineNo">2504</span>        return delegate;<a name="line.2504"></a>
<span class="sourceLineNo">2505</span>      }<a name="line.2505"></a>
<span class="sourceLineNo">2506</span><a name="line.2506"></a>
<span class="sourceLineNo">2507</span>      @Override public Collection&lt;V&gt; remove(Object o) {<a name="line.2507"></a>
<span class="sourceLineNo">2508</span>        Collection&lt;V&gt; output = FilteredMultimap.this.removeAll(o);<a name="line.2508"></a>
<span class="sourceLineNo">2509</span>        return output.isEmpty() ? null : output;<a name="line.2509"></a>
<span class="sourceLineNo">2510</span>      }<a name="line.2510"></a>
<span class="sourceLineNo">2511</span><a name="line.2511"></a>
<span class="sourceLineNo">2512</span>      @Override public void clear() {<a name="line.2512"></a>
<span class="sourceLineNo">2513</span>        FilteredMultimap.this.clear();<a name="line.2513"></a>
<span class="sourceLineNo">2514</span>      }<a name="line.2514"></a>
<span class="sourceLineNo">2515</span><a name="line.2515"></a>
<span class="sourceLineNo">2516</span>      Set&lt;K&gt; keySet;<a name="line.2516"></a>
<span class="sourceLineNo">2517</span><a name="line.2517"></a>
<span class="sourceLineNo">2518</span>      @Override public Set&lt;K&gt; keySet() {<a name="line.2518"></a>
<span class="sourceLineNo">2519</span>        return (keySet == null) ? keySet = new KeySet() : keySet;<a name="line.2519"></a>
<span class="sourceLineNo">2520</span>      }<a name="line.2520"></a>
<span class="sourceLineNo">2521</span><a name="line.2521"></a>
<span class="sourceLineNo">2522</span>      class KeySet extends Maps.KeySet&lt;K, Collection&lt;V&gt;&gt; {<a name="line.2522"></a>
<span class="sourceLineNo">2523</span>        @Override Map&lt;K, Collection&lt;V&gt;&gt; map() {<a name="line.2523"></a>
<span class="sourceLineNo">2524</span>          return AsMap.this;<a name="line.2524"></a>
<span class="sourceLineNo">2525</span>        }<a name="line.2525"></a>
<span class="sourceLineNo">2526</span><a name="line.2526"></a>
<span class="sourceLineNo">2527</span>        @Override public boolean remove(Object o) {<a name="line.2527"></a>
<span class="sourceLineNo">2528</span>           Collection&lt;V&gt; collection = delegate.get(o);<a name="line.2528"></a>
<span class="sourceLineNo">2529</span>           if (collection == null) {<a name="line.2529"></a>
<span class="sourceLineNo">2530</span>             return false;<a name="line.2530"></a>
<span class="sourceLineNo">2531</span>           }<a name="line.2531"></a>
<span class="sourceLineNo">2532</span>           collection.clear();<a name="line.2532"></a>
<span class="sourceLineNo">2533</span>           return true;<a name="line.2533"></a>
<span class="sourceLineNo">2534</span>        }<a name="line.2534"></a>
<span class="sourceLineNo">2535</span><a name="line.2535"></a>
<span class="sourceLineNo">2536</span>        @Override public boolean removeAll(Collection&lt;?&gt; c) {<a name="line.2536"></a>
<span class="sourceLineNo">2537</span>          return Sets.removeAllImpl(this, c.iterator());<a name="line.2537"></a>
<span class="sourceLineNo">2538</span>        }<a name="line.2538"></a>
<span class="sourceLineNo">2539</span><a name="line.2539"></a>
<span class="sourceLineNo">2540</span>        @Override public boolean retainAll(final Collection&lt;?&gt; c) {<a name="line.2540"></a>
<span class="sourceLineNo">2541</span>          Predicate&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt; removalPredicate<a name="line.2541"></a>
<span class="sourceLineNo">2542</span>              = new Predicate&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt;() {<a name="line.2542"></a>
<span class="sourceLineNo">2543</span>                @Override public boolean apply(Map.Entry&lt;K, Collection&lt;V&gt;&gt; entry) {<a name="line.2543"></a>
<span class="sourceLineNo">2544</span>                  return !c.contains(entry.getKey());<a name="line.2544"></a>
<span class="sourceLineNo">2545</span>                }<a name="line.2545"></a>
<span class="sourceLineNo">2546</span>              };<a name="line.2546"></a>
<span class="sourceLineNo">2547</span>          return removeEntriesIf(removalPredicate);<a name="line.2547"></a>
<span class="sourceLineNo">2548</span>        }<a name="line.2548"></a>
<span class="sourceLineNo">2549</span>      }<a name="line.2549"></a>
<span class="sourceLineNo">2550</span><a name="line.2550"></a>
<span class="sourceLineNo">2551</span>      Values asMapValues;<a name="line.2551"></a>
<span class="sourceLineNo">2552</span><a name="line.2552"></a>
<span class="sourceLineNo">2553</span>      @Override public Collection&lt;Collection&lt;V&gt;&gt; values() {<a name="line.2553"></a>
<span class="sourceLineNo">2554</span>        return (asMapValues == null) ? asMapValues = new Values() : asMapValues;<a name="line.2554"></a>
<span class="sourceLineNo">2555</span>      }<a name="line.2555"></a>
<span class="sourceLineNo">2556</span><a name="line.2556"></a>
<span class="sourceLineNo">2557</span>      class Values extends Maps.Values&lt;K, Collection&lt;V&gt;&gt; {<a name="line.2557"></a>
<span class="sourceLineNo">2558</span>        @Override Map&lt;K, Collection&lt;V&gt;&gt; map() {<a name="line.2558"></a>
<span class="sourceLineNo">2559</span>          return AsMap.this;<a name="line.2559"></a>
<span class="sourceLineNo">2560</span>        }<a name="line.2560"></a>
<span class="sourceLineNo">2561</span><a name="line.2561"></a>
<span class="sourceLineNo">2562</span>        @Override public boolean remove(Object o) {<a name="line.2562"></a>
<span class="sourceLineNo">2563</span>          for (Collection&lt;V&gt; collection : this) {<a name="line.2563"></a>
<span class="sourceLineNo">2564</span>            if (collection.equals(o)) {<a name="line.2564"></a>
<span class="sourceLineNo">2565</span>              collection.clear();<a name="line.2565"></a>
<span class="sourceLineNo">2566</span>              return true;<a name="line.2566"></a>
<span class="sourceLineNo">2567</span>            }<a name="line.2567"></a>
<span class="sourceLineNo">2568</span>          }<a name="line.2568"></a>
<span class="sourceLineNo">2569</span>          return false;<a name="line.2569"></a>
<span class="sourceLineNo">2570</span>        }<a name="line.2570"></a>
<span class="sourceLineNo">2571</span><a name="line.2571"></a>
<span class="sourceLineNo">2572</span>        @Override public boolean removeAll(final Collection&lt;?&gt; c) {<a name="line.2572"></a>
<span class="sourceLineNo">2573</span>          Predicate&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt; removalPredicate<a name="line.2573"></a>
<span class="sourceLineNo">2574</span>              = new Predicate&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt;() {<a name="line.2574"></a>
<span class="sourceLineNo">2575</span>                @Override public boolean apply(Map.Entry&lt;K, Collection&lt;V&gt;&gt; entry) {<a name="line.2575"></a>
<span class="sourceLineNo">2576</span>                  return c.contains(entry.getValue());<a name="line.2576"></a>
<span class="sourceLineNo">2577</span>                }<a name="line.2577"></a>
<span class="sourceLineNo">2578</span>              };<a name="line.2578"></a>
<span class="sourceLineNo">2579</span>          return removeEntriesIf(removalPredicate);<a name="line.2579"></a>
<span class="sourceLineNo">2580</span>        }<a name="line.2580"></a>
<span class="sourceLineNo">2581</span><a name="line.2581"></a>
<span class="sourceLineNo">2582</span>        @Override public boolean retainAll(final Collection&lt;?&gt; c) {<a name="line.2582"></a>
<span class="sourceLineNo">2583</span>          Predicate&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt; removalPredicate<a name="line.2583"></a>
<span class="sourceLineNo">2584</span>              = new Predicate&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt;() {<a name="line.2584"></a>
<span class="sourceLineNo">2585</span>                @Override public boolean apply(Map.Entry&lt;K, Collection&lt;V&gt;&gt; entry) {<a name="line.2585"></a>
<span class="sourceLineNo">2586</span>                  return !c.contains(entry.getValue());<a name="line.2586"></a>
<span class="sourceLineNo">2587</span>                }<a name="line.2587"></a>
<span class="sourceLineNo">2588</span>              };<a name="line.2588"></a>
<span class="sourceLineNo">2589</span>          return removeEntriesIf(removalPredicate);<a name="line.2589"></a>
<span class="sourceLineNo">2590</span>        }<a name="line.2590"></a>
<span class="sourceLineNo">2591</span>      }<a name="line.2591"></a>
<span class="sourceLineNo">2592</span><a name="line.2592"></a>
<span class="sourceLineNo">2593</span>      EntrySet entrySet;<a name="line.2593"></a>
<span class="sourceLineNo">2594</span><a name="line.2594"></a>
<span class="sourceLineNo">2595</span>      @Override public Set&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt; entrySet() {<a name="line.2595"></a>
<span class="sourceLineNo">2596</span>        return (entrySet == null) ? entrySet = new EntrySet(super.entrySet()) : entrySet;<a name="line.2596"></a>
<span class="sourceLineNo">2597</span>      }<a name="line.2597"></a>
<span class="sourceLineNo">2598</span><a name="line.2598"></a>
<span class="sourceLineNo">2599</span>      class EntrySet extends Maps.EntrySet&lt;K, Collection&lt;V&gt;&gt; {<a name="line.2599"></a>
<span class="sourceLineNo">2600</span>        Set&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt; delegateEntries;<a name="line.2600"></a>
<span class="sourceLineNo">2601</span><a name="line.2601"></a>
<span class="sourceLineNo">2602</span>        public EntrySet(Set&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt; delegateEntries) {<a name="line.2602"></a>
<span class="sourceLineNo">2603</span>          this.delegateEntries = delegateEntries;<a name="line.2603"></a>
<span class="sourceLineNo">2604</span>        }<a name="line.2604"></a>
<span class="sourceLineNo">2605</span><a name="line.2605"></a>
<span class="sourceLineNo">2606</span>        @Override Map&lt;K, Collection&lt;V&gt;&gt; map() {<a name="line.2606"></a>
<span class="sourceLineNo">2607</span>          return AsMap.this;<a name="line.2607"></a>
<span class="sourceLineNo">2608</span>        }<a name="line.2608"></a>
<span class="sourceLineNo">2609</span><a name="line.2609"></a>
<span class="sourceLineNo">2610</span>        @Override public Iterator&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt; iterator() {<a name="line.2610"></a>
<span class="sourceLineNo">2611</span>          return delegateEntries.iterator();<a name="line.2611"></a>
<span class="sourceLineNo">2612</span>        }<a name="line.2612"></a>
<span class="sourceLineNo">2613</span><a name="line.2613"></a>
<span class="sourceLineNo">2614</span>        @Override public boolean remove(Object o) {<a name="line.2614"></a>
<span class="sourceLineNo">2615</span>          if (o instanceof Entry) {<a name="line.2615"></a>
<span class="sourceLineNo">2616</span>            Entry&lt;?, ?&gt; entry = (Entry&lt;?, ?&gt;) o;<a name="line.2616"></a>
<span class="sourceLineNo">2617</span>            Collection&lt;V&gt; collection = delegate.get(entry.getKey());<a name="line.2617"></a>
<span class="sourceLineNo">2618</span>            if (collection != null &amp;&amp; collection.equals(entry.getValue())) {<a name="line.2618"></a>
<span class="sourceLineNo">2619</span>              collection.clear();<a name="line.2619"></a>
<span class="sourceLineNo">2620</span>              return true;<a name="line.2620"></a>
<span class="sourceLineNo">2621</span>            }<a name="line.2621"></a>
<span class="sourceLineNo">2622</span>          }<a name="line.2622"></a>
<span class="sourceLineNo">2623</span>          return false;<a name="line.2623"></a>
<span class="sourceLineNo">2624</span>        }<a name="line.2624"></a>
<span class="sourceLineNo">2625</span><a name="line.2625"></a>
<span class="sourceLineNo">2626</span>        @Override public boolean removeAll(Collection&lt;?&gt; c) {<a name="line.2626"></a>
<span class="sourceLineNo">2627</span>          return Sets.removeAllImpl(this, c);<a name="line.2627"></a>
<span class="sourceLineNo">2628</span>        }<a name="line.2628"></a>
<span class="sourceLineNo">2629</span><a name="line.2629"></a>
<span class="sourceLineNo">2630</span>        @Override public boolean retainAll(final Collection&lt;?&gt; c) {<a name="line.2630"></a>
<span class="sourceLineNo">2631</span>          Predicate&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt; removalPredicate<a name="line.2631"></a>
<span class="sourceLineNo">2632</span>              = new Predicate&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt;() {<a name="line.2632"></a>
<span class="sourceLineNo">2633</span>                @Override public boolean apply(Map.Entry&lt;K, Collection&lt;V&gt;&gt; entry) {<a name="line.2633"></a>
<span class="sourceLineNo">2634</span>                  return !c.contains(entry);<a name="line.2634"></a>
<span class="sourceLineNo">2635</span>                }<a name="line.2635"></a>
<span class="sourceLineNo">2636</span>              };<a name="line.2636"></a>
<span class="sourceLineNo">2637</span>          return removeEntriesIf(removalPredicate);<a name="line.2637"></a>
<span class="sourceLineNo">2638</span>        }<a name="line.2638"></a>
<span class="sourceLineNo">2639</span>      }<a name="line.2639"></a>
<span class="sourceLineNo">2640</span>    }<a name="line.2640"></a>
<span class="sourceLineNo">2641</span><a name="line.2641"></a>
<span class="sourceLineNo">2642</span>    AbstractMultiset&lt;K&gt; keys;<a name="line.2642"></a>
<span class="sourceLineNo">2643</span><a name="line.2643"></a>
<span class="sourceLineNo">2644</span>    @Override public Multiset&lt;K&gt; keys() {<a name="line.2644"></a>
<span class="sourceLineNo">2645</span>      return (keys == null) ? keys = new Keys() : keys;<a name="line.2645"></a>
<span class="sourceLineNo">2646</span>    }<a name="line.2646"></a>
<span class="sourceLineNo">2647</span><a name="line.2647"></a>
<span class="sourceLineNo">2648</span>    class Keys extends Multimaps.Keys&lt;K, V&gt; {<a name="line.2648"></a>
<span class="sourceLineNo">2649</span>      @Override Multimap&lt;K, V&gt; multimap() {<a name="line.2649"></a>
<span class="sourceLineNo">2650</span>        return FilteredMultimap.this;<a name="line.2650"></a>
<span class="sourceLineNo">2651</span>      }<a name="line.2651"></a>
<span class="sourceLineNo">2652</span><a name="line.2652"></a>
<span class="sourceLineNo">2653</span>      @Override public int remove(Object o, int occurrences) {<a name="line.2653"></a>
<span class="sourceLineNo">2654</span>        checkArgument(occurrences &gt;= 0);<a name="line.2654"></a>
<span class="sourceLineNo">2655</span>        Collection&lt;V&gt; values = unfiltered.asMap().get(o);<a name="line.2655"></a>
<span class="sourceLineNo">2656</span>        if (values == null) {<a name="line.2656"></a>
<span class="sourceLineNo">2657</span>          return 0;<a name="line.2657"></a>
<span class="sourceLineNo">2658</span>        }<a name="line.2658"></a>
<span class="sourceLineNo">2659</span>        int priorCount = 0;<a name="line.2659"></a>
<span class="sourceLineNo">2660</span>        int removed = 0;<a name="line.2660"></a>
<span class="sourceLineNo">2661</span>        Iterator&lt;V&gt; iterator = values.iterator();<a name="line.2661"></a>
<span class="sourceLineNo">2662</span>        while (iterator.hasNext()) {<a name="line.2662"></a>
<span class="sourceLineNo">2663</span>          if (satisfiesPredicate(o, iterator.next())) {<a name="line.2663"></a>
<span class="sourceLineNo">2664</span>            priorCount++;<a name="line.2664"></a>
<span class="sourceLineNo">2665</span>            if (removed &lt; occurrences) {<a name="line.2665"></a>
<span class="sourceLineNo">2666</span>              iterator.remove();<a name="line.2666"></a>
<span class="sourceLineNo">2667</span>              removed++;<a name="line.2667"></a>
<span class="sourceLineNo">2668</span>            }<a name="line.2668"></a>
<span class="sourceLineNo">2669</span>          }<a name="line.2669"></a>
<span class="sourceLineNo">2670</span>        }<a name="line.2670"></a>
<span class="sourceLineNo">2671</span>        return priorCount;<a name="line.2671"></a>
<span class="sourceLineNo">2672</span>      }<a name="line.2672"></a>
<span class="sourceLineNo">2673</span><a name="line.2673"></a>
<span class="sourceLineNo">2674</span>      @Override Set&lt;Multiset.Entry&lt;K&gt;&gt; createEntrySet() {<a name="line.2674"></a>
<span class="sourceLineNo">2675</span>        return new EntrySet();<a name="line.2675"></a>
<span class="sourceLineNo">2676</span>      }<a name="line.2676"></a>
<span class="sourceLineNo">2677</span><a name="line.2677"></a>
<span class="sourceLineNo">2678</span>      class EntrySet extends Multimaps.Keys&lt;K, V&gt;.KeysEntrySet {<a name="line.2678"></a>
<span class="sourceLineNo">2679</span>        @Override<a name="line.2679"></a>
<span class="sourceLineNo">2680</span>        public boolean removeAll(final Collection&lt;?&gt; c) {<a name="line.2680"></a>
<span class="sourceLineNo">2681</span>          return Sets.removeAllImpl(this, c.iterator());<a name="line.2681"></a>
<span class="sourceLineNo">2682</span>        }<a name="line.2682"></a>
<span class="sourceLineNo">2683</span><a name="line.2683"></a>
<span class="sourceLineNo">2684</span>        @Override public boolean retainAll(final Collection&lt;?&gt; c) {<a name="line.2684"></a>
<span class="sourceLineNo">2685</span>          Predicate&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt; removalPredicate<a name="line.2685"></a>
<span class="sourceLineNo">2686</span>              = new Predicate&lt;Map.Entry&lt;K, Collection&lt;V&gt;&gt;&gt;() {<a name="line.2686"></a>
<span class="sourceLineNo">2687</span>                @Override public boolean apply(Map.Entry&lt;K, Collection&lt;V&gt;&gt; entry) {<a name="line.2687"></a>
<span class="sourceLineNo">2688</span>                  Multiset.Entry&lt;K&gt; multisetEntry<a name="line.2688"></a>
<span class="sourceLineNo">2689</span>                      = Multisets.immutableEntry(entry.getKey(), entry.getValue().size());<a name="line.2689"></a>
<span class="sourceLineNo">2690</span>                  return !c.contains(multisetEntry);<a name="line.2690"></a>
<span class="sourceLineNo">2691</span>                }<a name="line.2691"></a>
<span class="sourceLineNo">2692</span>              };<a name="line.2692"></a>
<span class="sourceLineNo">2693</span>          return removeEntriesIf(removalPredicate);<a name="line.2693"></a>
<span class="sourceLineNo">2694</span>        }<a name="line.2694"></a>
<span class="sourceLineNo">2695</span>      }<a name="line.2695"></a>
<span class="sourceLineNo">2696</span>    }<a name="line.2696"></a>
<span class="sourceLineNo">2697</span>  }<a name="line.2697"></a>
<span class="sourceLineNo">2698</span><a name="line.2698"></a>
<span class="sourceLineNo">2699</span>  // TODO(jlevy): Create methods that filter a SetMultimap or SortedSetMultimap.<a name="line.2699"></a>
<span class="sourceLineNo">2700</span>}<a name="line.2700"></a>




























































</pre>
</div>
</body>
</html>
